Site icon Full-Stack

Python ZIP File Tutorial

Managing ZIP files is a common task when working with data or file compression—and Python makes it surprisingly easy. Whether you need to compress files for storage or extract content from a ZIP archive, Python’s built-in zipfile module has you covered.

In this guide, you’ll learn how to read, create, and extract ZIP files using simple Python scripts.


Why Work with ZIP Files?


Step 1: Import the zipfile Module

Python includes the zipfile module by default—no installation needed.

python

Copy code

import zipfile


Step 2: Extracting a ZIP File

python

Copy code

with zipfile.ZipFile(‘example.zip’, ‘r’) as zip_ref:

    zip_ref.extractall(‘output_folder’)

✅ This extracts all contents of example.zip into the output_folder.

You can also extract specific files:

python

Copy code

zip_ref.extract(‘document.txt’, ‘output_folder’)


Step 3: Creating a ZIP File

python

Copy code

with zipfile.ZipFile(‘new_archive.zip’, ‘w’) as zipf:

    zipf.write(‘file1.txt’)

    zipf.write(‘file2.txt’)

🔁 The ‘w’ mode creates a new ZIP file. Use ‘a’ to append files.

You can also set the compression method:

python

Copy code

zipf = zipfile.ZipFile(‘compressed.zip’, ‘w’, zipfile.ZIP_DEFLATED)


Step 4: Reading Contents of a ZIP File

python

Copy code

with zipfile.ZipFile(‘example.zip’, ‘r’) as zipf:

    print(zipf.namelist())

This prints a list of all file names inside the ZIP.


Practical Example: Compress a Folder

python

Copy code

import os

def zip_folder(folder_path, output_path):

    with zipfile.ZipFile(output_path, ‘w’, zipfile.ZIP_DEFLATED) as zipf:

        for foldername, subfolders, filenames in os.walk(folder_path):

            for filename in filenames:

                file_path = os.path.join(foldername, filename)

                arcname = os.path.relpath(file_path, folder_path)

                zipf.write(file_path, arcname)


Real-World Use Cases


Try It Yourself

Practice by creating a ZIP archive of multiple .txt files and extracting them with Python. These hands-on exercises help reinforce your understanding of file handling in Python.

🚀 Want more real-world Python projects and automation tips?
👉 https://www.thefullstack.co.in/courses/

You might be like this:-

What is AWS Lambda?A Beginner’s Guide to Serverless Computing in 2025

Java vs. Kotlin: Which One Should You Learn for Backend Development?

Where to Find Your Salesforce Organization ID

How Salesforce Stands Out from Other CRMs

One of the key benefits of using ZIP files is the ability to compress and organize large datasets, making it easier to share and store data. By using Python’s zipfile module, you can automate tasks such as backing up files, sending compressed attachments via email, and managing server or local files. Additionally, ZIP files can be used to package datasets, logs, or reports, making it easier to transfer and store data.

As you become more comfortable working with ZIP files in Python, you can explore more advanced topics such as using different compression methods, working with encrypted ZIP files, and integrating ZIP file handling with other Python libraries and frameworks. With practice and experience, you can unlock the full potential of Python’s zipfile module and take your data management and automation skills to the next level. Try experimenting with different use cases and scenarios to see how ZIP files can be used to streamline your workflows and improve your productivity.

Frequently Asked Questions

What is the purpose of the ZIP file module in Python?

The ZIP file module in Python is used to compress and decompress files, allowing for efficient storage and transfer of data. This module provides an easy-to-use interface for creating, reading, and writing ZIP files. By using the ZIP file module, you can significantly reduce the size of your files and improve data transfer times.

How do I create a ZIP file in Python?

To create a ZIP file in Python, you can use the zipfile module, which provides the ZipFile class for creating and writing ZIP files. You can create a new ZIP file by specifying the file name and mode, and then use the write method to add files to the ZIP archive. The ZIP file is created when you close the ZipFile object.

Can I add multiple files to a single ZIP file in Python?

Yes, you can add multiple files to a single ZIP file in Python using the write method of the ZipFile class. This method allows you to specify the file name and the compression type, and you can call it multiple times to add different files to the same ZIP archive. You can also use the writestr method to add file-like objects to the ZIP file.

How do I extract files from a ZIP file in Python?

To extract files from a ZIP file in Python, you can use the extract or extractall method of the ZipFile class. The extract method allows you to extract a single file from the ZIP archive, while the extractall method extracts all files to a specified directory. You can also use the read method to read the contents of a file in the ZIP archive without extracting it.

Can I use the ZIP file module to create password-protected ZIP files in Python?

Yes, you can use the ZIP file module to create password-protected ZIP files in Python, but this requires using the setpassword method of the ZipFile class, which is only available in ZIP files with encryption. To use encryption, you need to use the zipfile.ZIP_DEFLATED mode and specify a password when creating the ZIP file. However, note that not all ZIP file viewers support encrypted ZIP files.

Exit mobile version