- Data Compression and Archiving¶
- Previous topic
- Next topic
- This Page
- Navigation
- How to zip files and directories with Python
- Learn how to zip files and directories with Python using the zipfile module for file compression.
- Import the modules
- Use zipfile to zip a single file
- Use zipfile to zip all files with a given suffix
- Use zipfile to compress a directory
- Use zipfile to compress several directories
- Use zipfile to compress all files in the current directory
Data Compression and Archiving¶
The modules described in this chapter support data compression with the zlib, gzip, bzip2 and lzma algorithms, and the creation of ZIP- and tar-format archives. See also Archiving operations provided by the shutil module.
- zlib — Compression compatible with gzip
- gzip — Support for gzip files
- Examples of usage
- Command Line Interface
- Command line options
- (De)compression of files
- Incremental (de)compression
- One-shot (de)compression
- Examples of usage
- Reading and writing compressed files
- Compressing and decompressing data in memory
- Miscellaneous
- Specifying custom filter chains
- Examples
- ZipFile Objects
- Path Objects
- PyZipFile Objects
- ZipInfo Objects
- Command-Line Interface
- Command-line options
- From file itself
- File System limitations
- Resources limitations
- Interruption
- Default behaviors of extraction
- TarFile Objects
- TarInfo Objects
- Extraction filters
- Default named filters
- Filter errors
- Hints for further verification
- Supporting older Python versions
- Stateful extraction filter example
- Command-line options
Previous topic
Next topic
This Page
Navigation
© Copyright 2001-2023, Python Software Foundation.
This page is licensed under the Python Software Foundation License Version 2.
Examples, recipes, and other code in the documentation are additionally licensed under the Zero Clause BSD License.
See History and License for more information.The Python Software Foundation is a non-profit corporation. Please donate.
Last updated on Jul 21, 2023. Found a bug?
Created using Sphinx 4.5.0.How to zip files and directories with Python
Learn how to zip files and directories with Python using the zipfile module for file compression.
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