How To Work With ZIP Files In Python ?

How To Work With ZIP Files In Python featured image

Do you want to know how to work with ZIP Files in Python? Then, you have come to the right place.

It has In this article, we will get to know what ZIP Files are and how you can perform operations on them using Python Programming Language. Further, we will see what libraries and methods you can use for this purpose.

So, let’s get started!!

What is a ZIP File? What is a ZIP File used for?

Ever downloaded photos of your high school trip from your cloud storage like Google Drive or iCloud? Did you notice the extension or format of the downloads? Is it .zip?

What is that .zip format? Why do we use .zip?  Well, it is a format that compresses multiple files at one location. It is a common storage format that is widely used across numerous platforms.

ZIP files help to significantly reduce your download and upload times by helping you reduce the space and save storage. Hence, it is mostly used for data compression and archiving.

So, as a developer, wouldn’t it be better to understand how to work with ZIP files in Python? Lucky you! We are going to learn exactly that in today’s article.

Work with ZIP files using Python’s Built-in ‘zipfile’ Module.

Python has a vast and extensive set of libraries that can be used across various domains. Thus, making it one of the most scalable programming languages.

It allows you to access, manipulate, and store zip files using its built-in module. This module is named as ‘zipfile.’ Let us read more about it.

The zipfile module in Python is a built-in module that enables users to create zip files and perform operations like read, write, append, and list on a ZIP file.

One of the more advanced features of this module is that it can facilitate the decryption and encryption of ZIP files as well. However, the process is very slow.

To work with this module in your Python project, you need to import the module first. You can do so by using the following syntax.

import zipfile

After this, you will be able to use all the defined methods in this module in your code.

Now, let us see how to perform operations on ZIP files in Python.

Create a ZIP file containing multiple files in Python

Suppose you want to create a zip file that contains two text files. For this purpose, you need to include the os module along with the zip file module.

This is done, so that we can handle the operations related to the system. Below is the code that explains how it is done. First of all, we need to create Variables to store the file paths in the code. You must be aware also of deleting variables in Python. So, lets dive into the code now.

# import the required modules
import zipfile
import os
# create variables to store the file paths
file1_path = 'file1.txt'
file2_path = 'file2.txt'
zip_filename = ''

# create a zipfile object called zipf
# use the ZipFile method from the zipfile library to write the new files in the new zip
with zipfile.ZipFile(zip_filename, 'w') as zipf:
    zipf.write(file1_path, os.path.basename(file1_path))
    zipf.write(file2_path, os.path.basename(file2_path))


# check if the file is created or not
if os.path.exists(zip_filename):
   print("ZIP file successfully created!")
   print("ZIP file cannot be created.")

We have used the write mode – ‘w’ to add the files in the zip file. This mode created a new zip and added files into it. You can also use this mode to add files in an existing zip.

The output printed on your terminal will be:

zip file successfully created
zip file successfully created

You will also find that the zip file called my_archive is created.

My archive
My archive

Extract from ZIP files in Python

Another operation that you can perform on a ZIP file is to extract all the data or files in a particular zip file.

To perform this, we will make use of the read mode – ‘r’ to read the files in the zip and use the method extract () to get the files.

Look at the program given below to extract files from a ZIP file using the Python programming language.

# import the necessary modules
import zipfile
import os

# Specify the name of the ZIP File from where you want to extract
zip_filename = ''

# Specify the directory where you want to extract the files
extract_dir = 'extracted_files'

# if the directory does not exist, make a new one
if not os.path.exists(extract_dir):

# Open the ZIP file in reading mode
with zipfile.ZipFile(zip_filename, 'r') as zipf:
    # Extract all files in the ZIP archive to the specified directory

print(f'All files extracted to {extract_dir}')

You can see the output below. You will also notice a new folder called extracted_files that contains file1.txt and file2.txt that we extracted from the ZIP file.

all files extracted to extracted files
all files extracted to extracted files

List all files in a ZIP file using Python

If you want to print the name and details of all the files in the ZIP, follow the code given below.

We will once again use the read mode for the same. However, there can be two methods to list the files in a ZIP.

i) If you just want to print the name and minor details like the date modified and size, you can use the following code.

with zipfile.ZipFile(zip_filename, 'r') as zipf:
    # printing all the contents of the zip file

Kindly note that this code is in continuation of the above-given code.

The output for the same will be –

seeing all files
seeing all files

ii) If you want to get a piece of more detailed information about the files, follow the below code in which we have used the namelist() and getinfo() methods.

# import the module datetime to get the last modified info.
import datetime

with zipfile.ZipFile(zip_filename, 'r') as zipf:
    # Get a list of all the files using namelist() method
    file_list = zipf.namelist()
    # printing the details of each file in the ZIP File
    for file_name in file_list:
        file_info = zipf.getinfo(file_name)
        print(f'File Name: {file_name}')
        print(f'Compressed Size: {file_info.compress_size} bytes')
        print(f'Uncompressed Size: {file_info.file_size} bytes')
        print(f'ZIP Version: {file_info.create_version}')
        print(f'Last Modified: {datetime.datetime(*file_info.date_time)}')

This code is also in continuation of the above code. Here, we have also used the datetime module. Look at the output below to understand the program.

datetime in files
datetime in files

Append in a ZIP File using Python

What if you want to add new files in an already created ZIP file? Will you have to extract all items and then compress new and old files altogether again?

Too much work, right?

Thankfully Python has a solution for this! Next in how to work with ZIP Files in Python, we will see how we can append new files in a ZIP File.

For this, we will have to open the zip file in the append mode. We will append it to, which we created in the first section, in this example. See how it works below.

new_file_path = 'file3.txt'

# Opening in append ('a') mode
with zipfile.ZipFile(zip_filename, 'a') as zipf:
    # Append the new file to the ZIP

if os.path.exists(zip_filename):
   print(f'{new_file_path} has been appended to {zip_filename}')
   print(f"New file cannot be appended to {zip_filename}")

Find the output of the code below.

appending the text files
appending the text files

By now, we hope you have understood how to work with ZIP Files in Python. Keep in mind, that understanding how to work on different file formats helps you to develop and sharpen your skills.


Join our list

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.

Thank you for signup. A Confirmation Email has been sent to your Email Address.

Something went wrong.

Within the bustling realm of data science, our editorial team stands as a collective force of learning and exploration. Meet the dynamic minds behind the scenes—Sukesh, Abhishek, and other Authors. As passionate data science learners, they collectively weave a tapestry of insights, discoveries, and shared learning experiences.
Thank you For sharing.We appreciate your support. Don't Forget to LIKE and FOLLOW our SITE to keep UPDATED with Data Science Learner