{"id":11423,"date":"2020-12-15T17:16:43","date_gmt":"2020-12-15T17:16:43","guid":{"rendered":"https:\/\/www.askpython.com\/?p=11423"},"modified":"2020-12-15T17:18:11","modified_gmt":"2020-12-15T17:18:11","slug":"zipfile-module","status":"publish","type":"post","link":"https:\/\/www.askpython.com\/python-modules\/zipfile-module","title":{"rendered":"zipfile Module &#8211; Working with ZIP files in Python."},"content":{"rendered":"\n<p>In this tutorial, we will see what zip files are and we will implement  code in python to automate working with zip files with the <code>zipfile<\/code> module. We will see how to create compressed and uncompressed  zip files and extract files from zip files. We will also see how to append new files to already created zip file.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What is a zip file?<\/h2>\n\n\n\n<p>ZIP is an archive file format that supports lossless data compression. It may contain one or more files or folders that may or may not be compressed. ZIP files are used as a base file format by many programs and use .zip file extension.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">How to create a zip file with the zipfile module without compression?<\/h2>\n\n\n\n<p>In Python, we can create zip files using the <code>ZipFile()<\/code> method of the zipfile module. We can then add other files to the zip file. The following screenshot shows the files in the folder before creating a zipped file.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"259\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/12\/folder-before-zipping-1024x259.png\" alt=\"Folder Before Zipping\" class=\"wp-image-11449\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/12\/folder-before-zipping-1024x259.png 1024w, https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/12\/folder-before-zipping-300x76.png 300w, https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/12\/folder-before-zipping-768x194.png 768w, https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/12\/folder-before-zipping.png 1202w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption>Folder Before Zipping<\/figcaption><\/figure>\n\n\n\n<p>Next we&#8217;ll look at the code. Here the <code>ZipFile()<\/code> method takes filename of the zip file to be created as first argument and &#8220;w&#8221; for  opening file in write mode. <\/p>\n\n\n\n<p><strong>Don&#8217;t forget to close the file in the end of the program.<\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n#import module\nimport zipfile\n\n#declare filename\nfilename=&quot;zip_tutorial\/tutorial.zip&quot;\n\n# create a ZipFile object by opening file\nzip_obj= zipfile.ZipFile(filename,&quot;w&quot;)\n\n#add files to zip file\nzip_obj.write(&quot;zip_tutorial\/plane.xml&quot;)\nzip_obj.write(&quot;zip_tutorial\/sample.txt&quot;)\nzip_obj.write(&quot;zip_tutorial\/person.ini&quot;)\n\n#close the file object\nzip_obj.close()\n<\/pre><\/div>\n\n\n<p>The following figure shows the Content of the folder after creation of the ZIP file.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"258\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/12\/folder-after-zipping-1024x258.png\" alt=\"Folder After Zipping\" class=\"wp-image-11450\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/12\/folder-after-zipping-1024x258.png 1024w, https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/12\/folder-after-zipping-300x76.png 300w, https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/12\/folder-after-zipping-768x193.png 768w, https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/12\/folder-after-zipping.png 1200w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption>Folder After Zipping<\/figcaption><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">How to create a compressed zip file with the zipfile module?<\/h2>\n\n\n\n<p>To create a compressed zip file, we just need to provide the algorithm with which we want to compress the files as an argument to the <code>ZipFile() <\/code>method. <\/p>\n\n\n\n<p>Generally, we use <code>DEFLATED<\/code> algorithm to compress the files while creating zip files. The following figure shows the content of the folder before the creation of a compressed zip file.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"259\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/12\/folder-before-zipping-1-1024x259.png\" alt=\"Folder Before Zipping 1\" class=\"wp-image-11451\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/12\/folder-before-zipping-1-1024x259.png 1024w, https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/12\/folder-before-zipping-1-300x76.png 300w, https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/12\/folder-before-zipping-1-768x194.png 768w, https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/12\/folder-before-zipping-1.png 1202w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption>Folder Before compressed Zipping <\/figcaption><\/figure>\n\n\n\n<p>To create a compressed zip file we just need to add a compression argument specifying the algorithm for compression. Here we have used the zip_deflated algorithm hence<code> compression=zipfile.ZIP_DEFLATED<\/code> has been used while creating the ZipFile object.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n#import module\nimport zipfile\n\n#declare filename\nfilename=&quot;zip_tutorial\/tutorial.zip&quot;\n\n# create a ZipFile object by opening file\nzip_obj= zipfile.ZipFile(filename,&quot;w&quot;,compression=zipfile.ZIP_DEFLATED)\n\n#add files to zip file\nzip_obj.write(&quot;zip_tutorial\/plane.xml&quot;)\nzip_obj.write(&quot;zip_tutorial\/sample.txt&quot;)\nzip_obj.write(&quot;zip_tutorial\/person.ini&quot;)\n\n#close the file object\nzip_obj.close()\n<\/pre><\/div>\n\n\n<p>Following figure shows the content of the folder after the compressed zip file has been created.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"258\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/12\/folder-after-zipping-1-1024x258.png\" alt=\"Folder After Zipping 1\" class=\"wp-image-11452\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/12\/folder-after-zipping-1-1024x258.png 1024w, https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/12\/folder-after-zipping-1-300x76.png 300w, https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/12\/folder-after-zipping-1-768x193.png 768w, https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/12\/folder-after-zipping-1.png 1200w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption>Folder After Zipping 1<\/figcaption><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">How to check contents of a zipped folder without extracting the files in it?<\/h2>\n\n\n\n<p>To check the contents of a zip file, we can use the <code>namelist()<\/code> method of the <code>zipfile<\/code> module. <\/p>\n\n\n\n<p>Here <code>namelist()<\/code> method returns a list of names of files in the zip file when invoked on the ZipFile object. <\/p>\n\n\n\n<p><strong>Here we have opened the file in &#8220;read&#8221; mode hence &#8220;r&#8221; is given as a second argument to <code>ZipFile()<\/code>.<\/strong> We&#8217;ll use the <a href=\"https:\/\/www.askpython.com\/python\/python-for-loop\" class=\"rank-math-link\">for loop<\/a> here to loop over the contents of the zip file. <\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n#import module\nimport zipfile\n\n#declare filename\nfilename=&quot;zip_tutorial\/tutorial.zip&quot;\n\n# create a ZipFile object by opening file\nzip_obj= zipfile.ZipFile(filename,&quot;r&quot;)\n\n#print the content of zip file\nprint(&quot;Content of the ZIP file are: &quot;)\ncontent_list=zip_obj.namelist()\nfor fname in content_list:\n    print(fname)\n\n#close the file object\nzip_obj.close()\n<\/pre><\/div>\n\n\n<p>Output of above code snippet is:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nContent of the ZIP file are: \nzip_tutorial\/plane.xml\nzip_tutorial\/sample.txt\nzip_tutorial\/person.ini\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\">How to check metadata of a zipped file?<\/h2>\n\n\n\n<p>Metadata of a file is the data that contains the description of any file. It contains the information about date created, date modified , file size and other\u00a0 info. <\/p>\n\n\n\n<p>To get the metadata of a zip file, we can use the <code>infolist()<\/code> method of the <code>zipfile<\/code> module.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n#import module\nimport zipfile\n\n#declare filename\nfilename=&quot;zip_tutorial\/tutorial.zip&quot;\n\n# create a ZipFile object by opening file\nzip_obj= zipfile.ZipFile(filename,&quot;r&quot;)\n\n#print the metadata of zip file\nprint(&quot;Metadata of the ZIP file are: &quot;)\ncontent_list=zip_obj.infolist()\nfor info in content_list:\n    print(info)\n\n#close the file object\nzip_obj.close()\n<\/pre><\/div>\n\n\n<p>Output of above code is:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nMetadata of the ZIP file are: \n&lt;ZipInfo filename=&#039;zip_tutorial\/plane.xml&#039; compress_type=deflate filemode=&#039;-rw-rw-r--&#039; file_size=264 compress_size=174&gt;\n&lt;ZipInfo filename=&#039;zip_tutorial\/sample.txt&#039; compress_type=deflate filemode=&#039;-rw-rw-r--&#039; file_size=409 compress_size=215&gt;\n&lt;ZipInfo filename=&#039;zip_tutorial\/person.ini&#039; compress_type=deflate filemode=&#039;-rw-rw-r--&#039; file_size=183 compress_size=141&gt;\n<\/pre><\/div>\n\n\n<p>We can see that<code> infolist()<\/code> has returned info about filename, actual size, compressed size, compression algorithm, and file access modes of each file which are inside the zip file.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">How to append files directly into a zip file?<\/h2>\n\n\n\n<p>We can add extra files into a zip file directly using the <code>write()<\/code> method from the <strong>zipfile module<\/strong> as we have done while creating zip file. <\/p>\n\n\n\n<p><strong>The only difference is that we have to open the file in append mode hence &#8220;a&#8221; is passed as the second argument to <code>ZipFile()<\/code> method. <\/strong><\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n#import module\nimport zipfile\n\n#declare filename\nfilename=&quot;zip_tutorial\/tutorial.zip&quot;\n\n# create a ZipFile object by opening file\nzip_obj= zipfile.ZipFile(filename,&quot;a&quot;)\n\n#print the initial content of zip file\nprint(&quot;Initial Content of the ZIP file are: &quot;)\ncontent_list=zip_obj.namelist()\nfor fname in content_list:\n    print(fname)\n\n#Append a file to zip file\nzip_obj.write(&quot;zip_tutorial\/sampleoutput.txt&quot;)\n\n#close the file objecz\nzip_obj.close()\n\n#read final content of the file after appending\nnzip_obj= zipfile.ZipFile(filename,&quot;a&quot;)\n\n#print the initial content of zip file\nprint(&quot;Final Content of the ZIP file are: &quot;)\nncontent_list=nzip_obj.namelist()\nfor fname in ncontent_list:\n    print(fname)\n\n#close the file\nnzip_obj.close()\n<\/pre><\/div>\n\n\n<p>Output:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nInitial Content of the ZIP file are: \nzip_tutorial\/plane.xml\nzip_tutorial\/sample.txt\nzip_tutorial\/person.ini\nFinal Content of the ZIP file are: \nzip_tutorial\/plane.xml\nzip_tutorial\/sample.txt\nzip_tutorial\/person.ini\nzip_tutorial\/sampleoutput.txt\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\">How to extract a single file from a zipped folder?<\/h2>\n\n\n\n<p>To extract only a single file from a zipped folder, we can use the <code>extract()<\/code> method of the <code>zipfile<\/code> module. Here is the snap of the folder before extracting any file.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"257\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/12\/folder-before-unzipping-1024x257.png\" alt=\"Folder Before Unzipping\" class=\"wp-image-11453\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/12\/folder-before-unzipping-1024x257.png 1024w, https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/12\/folder-before-unzipping-300x75.png 300w, https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/12\/folder-before-unzipping-768x193.png 768w, https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/12\/folder-before-unzipping.png 1200w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption>Folder Before Unzipping<\/figcaption><\/figure>\n\n\n\n<p>The <code>extract()<\/code> method takes a filename as an argument and extracts the file in our working directory.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n#import module\nimport zipfile\n\n#declare filename\nfilename=&quot;zip_tutorial\/tutorial.zip&quot;\n\n# create a ZipFile object by opening file\nzip_obj= zipfile.ZipFile(filename,&quot;r&quot;)\n\n#extract file\nzip_obj.extract(&quot;zip_tutorial\/sampleoutput.txt&quot;)\n\n#close the zip file\nzip_obj.close()\n<\/pre><\/div>\n\n\n<p>Image given below shows a snap of the folder after the file has been extracted from zip file.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"258\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/12\/Extracted-file-from-ZIP-1024x258.png\" alt=\"Extracted File From ZIP\" class=\"wp-image-11454\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/12\/Extracted-file-from-ZIP-1024x258.png 1024w, https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/12\/Extracted-file-from-ZIP-300x76.png 300w, https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/12\/Extracted-file-from-ZIP-768x193.png 768w, https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/12\/Extracted-file-from-ZIP.png 1200w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption>Extracted File From ZIP<\/figcaption><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">How to extract all files from a zipped folder with the zipfile module?<\/h2>\n\n\n\n<p>To extract the whole zip folder instead of a single file, we can use the <code>extractall()<\/code> method of the <code>zipfile<\/code> module. The image given below shows the snap of the folder before extracting the contents of the zip file.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"257\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/12\/folder-before-unzipping-1-1024x257.png\" alt=\"Folder Before Unzipping 1\" class=\"wp-image-11455\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/12\/folder-before-unzipping-1-1024x257.png 1024w, https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/12\/folder-before-unzipping-1-300x75.png 300w, https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/12\/folder-before-unzipping-1-768x193.png 768w, https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/12\/folder-before-unzipping-1.png 1200w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption>Folder Before Unzipping whole file<\/figcaption><\/figure>\n\n\n\n<p>The <code>extractall()<\/code> method takes the name of the output file as its argument and extracts the entire contents of the zip file into the folder in our working directory.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n#import module\nimport zipfile\n\n#declare filename\nfilename=&quot;zip_tutorial\/tutorial.zip&quot;\n\n# create a ZipFile object by opening file\nzip_obj= zipfile.ZipFile(filename,&quot;r&quot;)\n\n#extract all files \nzip_obj.extractall(&quot;zip_tutorial\/tutorial_folder_after_extraction&quot;)\n\n#close the zip file\nzip_obj.close()\n<\/pre><\/div>\n\n\n<p>Below given image shows a snap of folder after whole content of the zip file has been extracted in an another folder.<\/p>\n\n\n\n<figure class=\"wp-block-image size-large\"><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"255\" src=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/12\/extracted-folder-from-ZIP-1024x255.png\" alt=\"Extracted Folder From ZIP\" class=\"wp-image-11456\" srcset=\"https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/12\/extracted-folder-from-ZIP-1024x255.png 1024w, https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/12\/extracted-folder-from-ZIP-300x75.png 300w, https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/12\/extracted-folder-from-ZIP-768x191.png 768w, https:\/\/www.askpython.com\/wp-content\/uploads\/2020\/12\/extracted-folder-from-ZIP.png 1200w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><figcaption>Extracted Folder From ZIP<\/figcaption><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>In this tutorial, we have seen what zip files are and how to create and manipulate zip files with the Python zipfile module. We also saw how to extract a single file as well as the whole content of the zip files. Happy Learning!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this tutorial, we will see what zip files are and we will implement code in python to automate working with zip files with the zipfile module. We will see how to create compressed and uncompressed zip files and extract files from zip files. We will also see how to append new files to already [&hellip;]<\/p>\n","protected":false},"author":20,"featured_media":11472,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-11423","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python-modules"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/11423","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/users\/20"}],"replies":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/comments?post=11423"}],"version-history":[{"count":0,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/11423\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media\/11472"}],"wp:attachment":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media?parent=11423"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/categories?post=11423"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/tags?post=11423"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}