{"id":7971,"date":"2021-01-21T16:30:04","date_gmt":"2021-01-21T11:00:04","guid":{"rendered":"https:\/\/www.h2kinfosys.com\/blog\/?p=7971"},"modified":"2021-01-21T16:30:05","modified_gmt":"2021-01-21T11:00:05","slug":"creating-and-writing-to-zip-files-in-python","status":"publish","type":"post","link":"https:\/\/www.h2kinfosys.com\/blog\/creating-and-writing-to-zip-files-in-python\/","title":{"rendered":"Creating and Writing to Zip Files in Python"},"content":{"rendered":"\n<p>Python allows you to work with zip files using the zip file library. You check a list of files in a zip file, write files to a file zip file, create files, and a host of other things with the library. In this tutorial, you will learn how to work with zip files in Python using the zip file library and os library. Let\u2019s get into it.\u00a0<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">What is a Zip file?<\/h2>\n\n\n\n<p>A zip file is an archive file that allows<a href=\"https:\/\/en.wikipedia.org\/wiki\/Lossless_compression\" class=\"rank-math-link\" rel=\"nofollow noopener\" target=\"_blank\"> lossless data compression<\/a>. A zip file can contain one or more files that have been compressed. This allows the files to be transferred as a whole without losing any fragment. Additionally, a compressed file has a smaller file size than the cumulative size of the individual files. So if you ask why we need zip files? Two reasons:<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li>To enhance transfer of files at maximum speed without losing any file fragment<\/li><li>To reduce the overall file size.&nbsp;<\/li><\/ul>\n\n\n\n<p>Now lets extract a zip file in <a href=\"https:\/\/www.h2kinfosys.com\/courses\/python-online-training\" class=\"rank-math-link\">python<\/a>.\u00a0<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Extracting Documents in a Zip File with Python<\/h2>\n\n\n\n<p>Lets say we need to extract the zip file named \u2018Documents\u2019&nbsp; on our local machine.&nbsp;<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh4.googleusercontent.com\/OUtFQjzRxRDA1AHs9-u1KSYBJ-QkPYMeGqIG-exf6_15zmgfEarjVwwnLdtufFsOBk6eTYnyqawqStXD1sRydqJPYy6RRMF-CQHX2Qnz8UlXnSEe_FpEF-xfRhc8bsv8ODJ0jdkI\" alt=\"\" title=\"\"><\/figure>\n\n\n\n<p>We run the following line of codes<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">#import the ZipFile class from the zipclass library\n<strong>from<\/strong> <strong>zipfile<\/strong> <strong>import<\/strong> ZipFile\n\n<strong>with<\/strong> ZipFile(r\"C:\\Users\\DELL\\Documents\\Documents.zip\", 'r') <strong>as<\/strong> zip:\n\u00a0\u00a0\u00a0\u00a0#check the file names in the zip file with the printdir() method\n\u00a0\u00a0\u00a0\u00a0<strong>print<\/strong>('These are the files in the zip file')\n\u00a0\u00a0\u00a0\u00a0zip.printdir()\n\u00a0\u00a0\u00a0\u00a0\n\u00a0\u00a0\u00a0\u00a0#extract the files in the zip file\n\u00a0\u00a0\u00a0\u00a0<strong>print<\/strong>('Files about to be extracted')\n\u00a0\u00a0\u00a0\u00a0zip.extractall()\n\u00a0\u00a0\u00a0\u00a0<strong>print<\/strong>('Files extraction complete')\n<\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh4.googleusercontent.com\/kaIl8Pecu18hcjjCGCl4rqKk4ymtRm5i_jmhm4NIKPaOkiFAleufPu3ktqe3fCTEeFKUUSDCy-rcFq7IYW1FiI9-83qbFfeN3OzidrQ3kwgkaHtIcFsj4BWm6HK4lAXK4Th1Mach\" alt=\"\" title=\"\"><\/figure>\n\n\n\n<p>As seen from the figure above, the code ran successfully. Let\u2019s see if the files were truly extracted.&nbsp;<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh3.googleusercontent.com\/fBB7OQzLOWz_Tw3XwPyPuZiIeF12Va9hkyNIUlXoh-4R3dcfM8QgHoAc0MJI2j_p8vad6Q5CJpAYoaHymnabxSj-aW-1y3e77dT2H6Prol9zWIg_-rZVtQNWgtuITgYDVQqNskJu\" alt=\"\" title=\"\"><\/figure>\n\n\n\n<p>All the files were extracted to our working directory. Let\u2019s break down the earlier line of code that performed the extraction.&nbsp;<\/p>\n\n\n\n<p>We started by importing the necessary module. Then the ZipFile module was instantiated as zip. The ZipFile class takes two arguments, the zip file path and the mode which could be write \u2018w\u2019, read \u2018r\u2019, append \u2018a\u2019 or exclusive create \u2018x\u2019. Since we only wanted to parse the file, we used the read mode.&nbsp;<\/p>\n\n\n\n<p>The printdir() method was used to check the file names and sizes of the zip file. Finally, the files were extracted using the extractall() method.&nbsp;<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Creating\/Writing to a Zip File with Python<\/h2>\n\n\n\n<p>We have seen how to extract files in a zip file. We can also write to a zip file using the write() method. The block of code below would write the 4 files we earlier extracted to an empty zip file.<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">#import the ZipFile class from the zipclass library\n<strong>from<\/strong> <strong>zipfile<\/strong> <strong>import<\/strong> ZipFile\n#list the files to compress\nfiles_to_compress = ['Day 1_Machine learning training.csv', 'Doc1.docx', 'salary.csv', 'test.py']\n\n<strong>with<\/strong> ZipFile('newzipfile.zip', 'w') <strong>as<\/strong> zip:\n\u00a0\u00a0\u00a0\u00a0#write the each file in a zip file called newzipfile.zip\n\u00a0\u00a0\u00a0\u00a0<strong>for<\/strong> each_file <strong>in<\/strong> files_to_compress:\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0zip.write(each_file)\n\u00a0\u00a0\u00a0\u00a0\n\u00a0\u00a0\u00a0\u00a0<strong>print<\/strong>('Compression complete')\n\u00a0\u00a0\u00a0\u00a0#print the files in the in the zip file\n\u00a0\u00a0\u00a0\u00a0zip.printdir()\n<\/pre>\n\n\n\n<p>Output:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh6.googleusercontent.com\/YXluXczDyzfN5yW1fLShdyAZZquLVz9rkDBxNVHKeSYP2ZTTP2D6vtVpBrxuBYrtQbvn9C5U3BjG_KmkiJvqF1pWkjhAvtr7hthjN-mn0fUwZjLyuSKbSxRyJSXvmxGsmCmV-NgK\" alt=\"\" title=\"\"><\/figure>\n\n\n\n<p>Let\u2019s see if the zip file was created.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh3.googleusercontent.com\/sYqDkzGpUU0eWFumzBxRcaQ03qfcXdvVxcCCNdgMzVTCcxIFxeEeqoFDiSrRM3zafA8ofa1g-NzUo3pJAMirOZibxAjyEO60pJypTUVlU_sSwb6wYyvc-ZM8Xff0__DOkXt0zLUv\" alt=\"\" title=\"\"><\/figure>\n\n\n\n<p>As seen, the zip file has been created.<\/p>\n\n\n\n<p>If you wish to write all files in a folder to a zip file, you can use the code below.\u00a0<\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">#import necessary libraries\n<strong>import<\/strong> <strong>os<\/strong>\n<strong>from<\/strong> <strong>os.path<\/strong> <strong>import<\/strong> join\n<strong>from<\/strong> <strong>zipfile<\/strong> <strong>import<\/strong> ZipFile\n\n# create an empty list to store the files\nfiles_list = []\n#use the walk method extract directory path, directory name and file name\n<strong>for<\/strong> dir_path, directory, files <strong>in<\/strong> os.walk('{directory of folders}'):\n\u00a0\u00a0\u00a0\u00a0<strong>for<\/strong> each_file <strong>in<\/strong> files:\u00a0\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0# create a full path by joining the parent path to each file name\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0each_filepath = join(dir_path, each_file)\u00a0\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0#add the each file path to the empty list\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0files_list.append(each_filepath)\n\n<strong>with<\/strong> ZipFile('{create path to store the zipped file}', 'w') <strong>as<\/strong> zip:\u00a0\n\u00a0\u00a0\u00a0\u00a0<strong>for<\/strong> file <strong>in<\/strong> files_list:\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0zip.write(file)\n<\/pre>\n\n\n\n<p>In conclusion, you have learnt how to extract files in a zip file and how to write a file(s) to a zip file. If you have any questions, feel free to leave them in the comment section, I\u2019d make sure to respond to them.\u00a0\u00a0<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Python allows you to work with zip files using the zip file library. You check a list of files in a zip file, write files to a file zip file, create files, and a host of other things with the library. In this tutorial, you will learn how to work with zip files in Python [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":7973,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[342],"tags":[],"class_list":["post-7971","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python-tutorials"],"_links":{"self":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts\/7971","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/comments?post=7971"}],"version-history":[{"count":0,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts\/7971\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/media\/7973"}],"wp:attachment":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/media?parent=7971"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/categories?post=7971"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/tags?post=7971"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}