How to zip files and directories with Python

Learn how to zip files and directories with Python using the zipfile module for file compression.

How to zip files and directories with Python
Picture by Mat Brown, Pexels.
4 minutes to read

The zipfile module in Python provides a way to compress files and directories into a single zip file. This is useful for reducing the size of files and directories that you want to share or transfer.

In this tutorial, you will discover how to zip files and directories with Python using the zipfile module. As well as compressing files and directories into zip files, the zipfile module can also be used to unzip files in Python.

Import the modules

To get started, open a Python script or Jupyter notebook and import the zipfile module and the glob module. Both of these modules are part of the Python standard library and do not need to be installed separately. The glob module is used to find files and directories that match a pattern, while the zipfile module is used to create and extract zip files.

import zipfile
import glob

Use zipfile to zip a single file

To compress a single file into a zip file archive we need to call the ZipFile class and pass the name of the zip file as an argument. We can then use the write() method to add the file to the zip file. The write() method takes the name of the file to add as an argument. The following example compresses the file named file1.csv and saves it as file1.zip.

with zipfile.ZipFile('file1.zip', 'w') as f:
    f.write('file1.csv')

Use zipfile to zip all files with a given suffix

By using the glob module, we can find all files with a given suffix and then compress them into a single zip file. The following example finds all files with the suffix .csv and then compresses them into a zip file named data.zip.

"""
Use zipfile to compress all files with the suffix .csv into a zip called data.zip
"""

with zipfile.ZipFile('data.zip', 'w') as f:
    for file in glob.glob('*.csv'):
        f.write(file)

Use zipfile to compress a directory

To compress all files in a directory into a zipfile we can again use the glob module. We’ll pass this the name of the directory we want to compress into a zip and then the asterisk wildcard to make it include all the files found within. We’ll then use the write() method to add each file to the zip file.

"""
Use zipfile to compress directory1 into a zip called directory1.zip
"""

with zipfile.ZipFile('directory1.zip', 'w') as f:
    for file in glob.glob('directory1/*'):
        f.write(file)

Use zipfile to compress several directories

We can use a similar approach to zip several directories into a single zip file. The following example compresses the directory1 and directory2 directories into a zip file named directories.zip.

"""
Use zipfile to compress directory1 and directory2 into a zip called directory1-and-2.zip
"""

with zipfile.ZipFile('directory1-and-2.zip', 'w') as f:
    for file in glob.glob('directory1/*') + glob.glob('directory2/*'):
        f.write(file)

Use zipfile to compress all files in the current directory

Finally, we can use the glob module to find all files in the current directory and then compress them into a zip file. The following example compresses all files in the current directory into a zip file named all-files.zip.

"""
Use zipfile to compress all files in the current directory
"""

with zipfile.ZipFile('all-files.zip', 'w') as f:
    for file in glob.glob('*'):
        f.write(file)

Matt Clarke, Wednesday, October 12, 2022

Matt Clarke Matt is an Ecommerce and Marketing Director who uses data science to help in his work. Matt has a Master's degree in Internet Retailing (plus two other Master's degrees in different fields) and specialises in the technical side of ecommerce and marketing.