Python zipfile is an inbuilt module that helps us to zip files. To zip the file means we can reduce the file size. The size of the file size will be reduced after zipping the file.

ZIP is an archive file format that supports lossless data compression. The lossless data compression is a type of compression algorithm that allows the original data to be entirely rebuilt from the compressed data.

How to Zip Multiple Files in Python

To zip multiple files in Python, use the zipfile.ZipFile() method. Iterate all the files that need to be zipped and use the write() method to write the final zipped file. Let’s understand this example step by step.

Step 1: Define three zip files.

To zip multiple files, we first have to define three files. Let’s say we have the following three files in our project directory.

  1. purchase.csv
  2. sales.csv
  3. marketing.csv

Step 2: Import the zipfile module and create a list.

To import any module in Python, use the import statement.

## app.py

import zipfile

The next step is to create a list and add these three csv files as list items.

list_files = ['sales.csv', 'purchase.csv', 'marketing.csv']

#python #python zipfile

How to Zip Multiple Files in Python
45.25 GEEK