In Python, you can use both the zipfile or shutil modules to zip and unzip files. We’re going to walk you through examples demonstrating how to zip files and entire folders using these modules.

Zipping Files and Folders

For the sake of executing examples in this tutorial, create a folder named files to zip and inside this folder create three files:

  1. my image.bmp
  2. my text.txt
  3. my word.docx

Once complete, your folder structure should look like this.

files to zip

Zip files with the zipfile module

To zip files using the zipfile module, first you have to import the zipfile module as shown below.

import zipfile

Next, create an object of the ZipFile class from the zipfile module. In the constructor of the ZipFile class, pass the name of the zipped file you want to make as the first argument. Pass w as the second argument to create a writeable zip file.

create_zip = zipfile.ZipFile("E:\my_docs.zip", "w")

Now that you’ve created your file, you can add as many files inside this zip file as you want. To do so, use the write() method. The first parameter to the write() method is the path to the file you want to zip. Pass zipfile.ZIP_DEFLATED as the value for the second parameter. The following script adds the “my_image.bmp” file to the “my_docs.zip” file you created earlier.

create_zip.write(r"E:\files to zip\my image.bmp", compress_type = zipfile.ZIP_DEFLATED)

Note: If you get a unicodeescape codec erroc, you’ll need to escape your backslash characters by putting two consecutive backslashes, like “C:\files to zip\my image.bmp”

In the same way, you can use the following script to add “my word.docx”, and “my text.txt” files to the zipped “my_docs.zip” file.

create_zip.write(r"E:\files to zip\my word.docx", compress_type = zipfile.ZIP_DEFLATED)
create_zip.write(r"E:\files to zip\my text.txt", compress_type = zipfile.ZIP_DEFLATED)

Once all the files are included, you need to close the opened ZipFile class object by calling the close() method, like this:

create_zip.close()

Now, if you go to the directory where you made your zip file, you should see a new zipped file “my_docs.zip”. Once you extract the contents of the file, you’ll see your compressed “my image.bmp”, “my text.txt”, and “my word.docx” files:

my zipped files

Zip folders with the shutil module

Though, you can use the zipfile module to zip individual files, you can’t zip an entire directory via the zipfile module. With the zipfile module, you must specify individual file names. If you want to compress all the files inside a directory using a single method, you can use the make_archive() method of the shutil module, as shown in the following script. You simply have to pass the name of the zipped folder that will be created as the first argument, the compression type (zip, tar, gz, etc.) as the second argument, and the name of the folder you want to zip as the third argument.

import shutil
shutil.make_archive("E:\my_docs_folder2", 'zip', r"E:\files to zip")

Note: Make sure you don’t place your zip file inside a folder you’re trying to create. This will perpetually build a nested zip file with constantly growing file size until your terminate the process or your entire hard drive becomes full.

Once the above script is executed, you’ll see a zipped file named my_docs_folder2.zip. Extracting the contents of the file using will reveal the directory structure showing the three zipped files.

my zipped files


Get Our Python Developer Kit for Free

I put together a Python Developer Kit with over 100 pre-built Python scripts covering data structures, Pandas, NumPy, Seaborn, machine learning, file processing, web scraping and a whole lot more - and I want you to have it for free. Enter your email address below and I'll send a copy your way.

Yes, I'll take a free Python Developer Kit

Unzipping Files and Folders

The zipfile and shutil modules can also be used for unzipping files and folders.

Unzipping with the zipfile module

As you did for zipping, for unzipping you first have to create an object of ZipFile class. However unlike zipping, for unzipping the first parameter is the path to the zipped file and the second parameter is the file permission which should be “r” (reading) in case of unzipping.

The following script creates an object of the ZipFile class for unzipping the “my_docs.zip” file. Next, you have to pass the path to the directory that will contain your unzipped file to the extractall() method as shown below:

import zipfile
extract_zip = zipfile.ZipFile("E:\my_docs.zip", "r")
extract_zip.extractall("E:\my_extracted_docs")

Once you execute the above script, you should see a directory named my_extracted_docs containing the contents of your unzipped files from the my_docs.zip file:

my extracted files

Unzipping with the shutil module

Calling the unpack_archive() method of the shutil module is required to unzip a file. The path to the zipped file is passed as the value for the first parameter of the unpack_archive() method. The path to the directory where you want to unzip your files is passed as the second parameter. Finally, the zip method i.e. zip, tar, gz etc., is passed as the third parameter value.

The following script unzips file from the “my_docs_folder2.zip” file and extract them in the “my_extracted_docs2” directory.

import shutil
shutil.unpack_archive("E:\my_docs_folder2.zip", "E:\my_extracted_docs2", "zip")

Once you execute the above script, you should see a directory named my_extracted_docs2 that contains your unzipped files from the my_docs.zip file, like this:

my extracted files


Get Our Python Developer Kit for Free

I put together a Python Developer Kit with over 100 pre-built Python scripts covering data structures, Pandas, NumPy, Seaborn, machine learning, file processing, web scraping and a whole lot more - and I want you to have it for free. Enter your email address below and I'll send a copy your way.

Yes, I'll take a free Python Developer Kit