Working with Python ZIP files in Python is easy and useful, especially when you need to save space or handle many files at once. With just a few code lines, you can create a Python ZIP file, read its contents, and even extract them using the built-in zipfile module. Let’s see some of the hands-on examples and learn the steps quickly.
In this article
Part 1. Getting Started with Python Zipfile Module
Python has a built-in tool called the zipfile module that lets you work with ZIP files. These are the compressed folders you see when downloading multiple files or backups. The zipfile module is a wholesome part of Python’s standard library. This means you don’t need to install anything. With this module, you can create ZIP files, open them, read what’s inside, and extract files from them.
ZIP uses lossless compression, which means you can fully recover the original files without losing any data. This helps save space and speeds up file transfers, especially over the internet.
You can open ZIP file Python to do the following:
- Reduce storage space
- Speed up file transfers
- Make it easy to share and organize multiple files.
Part 2. Best Way to Python Create ZIP File
Need to archive or share multiple files? Python makes it simple. Let's explore the best ways to create a ZIP file with Python using reliable and built-in modules.
1. Create an Uncompressed ZIP File in Python
An uncompressed ZIP file keeps the original file sizes the same. Since there's no compression, sharing it over a network offers no size advantage. It’s just like sharing the original files.
Python's shutil module can generate uncompressed ZIP archives. This is useful when you want to group several files into one archive without reducing their size.
Here's the basic syntax of shutil.make_archive():
shutil.make_archive(base_name, format, root_dir)
Example:
import shutil
import os.path
# Creating the ZIP file
archived = shutil.make_archive('E:/Zipped file', 'zip', 'E:/Folder to be zipped')
if os.path.exists('E:/Zipped file.zip'):
print(archived)
else:
print("ZIP file not created")
import os.path: Imports the os.path module to check if the ZIP file was created successfully.
shutil.make_archive('E:/Zipped file', 'zip', 'E:/Folder to be zipped'):
- Creates a ZIP Image named Zipped file.zip at E:/
- Compresses all contents of the folder located at E:/Folder to be zipped

This will create a file called my_archive.zip in the same directory, containing all the contents of my_folder.
2. Create a Compressed ZIP File in Python
Compressed ZIP files make the original folder or files smaller by using a compression algorithm. This helps speed up file transfers since the final ZIP file is much smaller than the original.
Python's built-in zipfile module lets you create compressed ZIP files using different compression methods. To do this, you can use the ZipFile() class. With the ‘with statement,’ you access and generate a ZIP file. Also, you can add more archives using the .write() method.
For example:
import os
from zipfile import ZipFile
# Create a ZipFile Object
with ZipFile('E:/Zipped file.zip', 'w') as zip_object:
# Adding files that need to be zipped
zip_object.write('E:/Folder to be zipped/Greetings.txt')
zip_object.write('E:/Folder to be zipped/Introduction.txt')
# Check to see if the zip file is created
if os.path.exists('E:/Zipped file.zip'):
print("ZIP file created")
else:
print("ZIP file not created")
- import os: Imports the os module to check if the ZIP file exists after creation.
- from zipfile import ZipFile: Inputs the ZipFile class from Python’s built-in zipfile module.
- with ZipFile('E:/Zipped file.zip', 'w') as zip_object:
- zip_object.write('E:/Folder to be zipped/Greetings.txt'): Adds the file Greetings.txt to the ZIP archive
- zip_object.write('E:/Folder to be zipped/Introduction.txt'): Adds the file Introduction.txt to the ZIP archive
- if os.path.exists('E:/Zipped file.zip'): Checks if the ZIP file was successfully created at the given location
Creates a new ZIP Image named Zipped file.zip at E:/
Opens it in write mode ('w'), ready to add files
The with statement guarantees the file is properly closed after use

Part 3. How to Open and Read ZIP Files in Python?
Python makes it simple to work with ZIP files using its built-in zipfile module. You can open ZIP files, see what's inside, and extract files without any extra tools. This is helpful when dealing with downloads or managing grouped files.
1. Open ZIP File Python Guide
To access a ZIP archive in Python, you utilize the built-in open() function. It takes two arguments:
- the name of the file
- the mode (like 'r' for reading)
Here's an example:
with open('ab.py', 'r') as f:
print(f.readlines())
- Opens the file ab.py in read mode ('r')
- Uses a with statement, which automatically closes the file when done
- Assigns the file to the variable f
- f.readlines() reads all lines and returns them as a list of strings
- print() displays the file's contents in the console

2. Read ZIP File Python Guide
To read a ZIP file in Python, use the built-in zipfile module.
import zipfile
with zipfile.ZipFile('your_file.zip') as myzip:
# Access files here
The common methods you can use:
- namelist(): Lists all Image names in the ZIP.
- infolist(): Gives details (like size) about each file.
- read(filename): Reads the content of a file.
- extract(filename, path): Extracts a file to a folder.
Example:
with ZipFile('aa.zip') as myzip:
with myzip.open('ac.txt') as myfile:
print(myfile.readline())

Part 4. How to Extract ZIP Files with Python
Python unzip files using 2 two methods: extractall() and extract().
1. extractall()
This method extracts all files from a zip archive. By default, it saves them in the current working directory, but you can set a different location using the file_path parameter.
ZipFile.extractall(file_path=None, members=None, pwd=None)
- file_path: Where to extract the files. If not set, files go to the current folder.
- members: List of specific files to extract. If not provided, all files are extracted.
- pwd: Password for encrypted files (optional).
2. extract()
This extracts a single ZIP file archive. Like extractall(), it saves to the current directory unless you provide a different path.
ZipFile.extract(member, file_path=None, pwd=None)
- ember: Name of the file to extract.
- file_path: Where to extract the file. Defaults to the current directory.
- pwd: Password for encrypted files (optional).
Here’s an example to extract all files or a specific file from a ZIP.
Step 1: Import the zipfile module.
Step 2: Create a ZipFile object.
Step 3: Use extractall() to extract all files to a specific folder.
Step 4: To extract a specific file, use the extract() method instead.
# importing the zipfile module
from zipfile import ZipFile
# loading the temp.zip and creating a zip object
with ZipFile("C:\\Users\\sai mohan pulamolu\\\
Desktop\\geeks_dir\\temp\\temp.zip", 'r') as zObject:
# Extracting all the members of the zip
# into a specific location.
zObject.extractall(
path="C:\\Users\\sai mohan pulamolu\\Desktop\\geeks_dir\\temp")

Here’s another example of extracting a specific (1) file from a ZIP.
Step 1: Import the zipfile module.
Step 2: Create a ZipFile object.
Step 3: Use the extract() method to extract a specific file by providing its name and the destination folder.
# importing the zipfile module
from zipfile import ZipFile
# loading the temp.zip and creating a zip object
with ZipFile("C:\\Users\\sai mohan pulamolu\\Desktop\
\\geeks_dir\\temp\\temp.zip", 'r') as zObject:
# Extracting specific file in the zip
# into a specific location.
zObject.extract(
"text1.txt", path="C:\\Users\\sai mohan pulamolu\\D\
esktop\\geeks_dir\\temp")
zObject.close()

Part 5. Handling Errors or Corrupted ZIP Files
ZIP archives in Python might encounter issues such as:
- BadZipFile: Raised when the zip file is corrupt or unreadable.
- RuntimeError: Occurs when trying to open an encrypted file without a password.
- UnicodeDecodeError: Happens when Image names contain unsupported characters or the zip uses a different encoding.
These errors can disrupt your workflow, especially if you're dealing with important or large files. To handle these issues, always wrap your ZIP operations in a try-except block to catch errors. Check if the file was fully downloaded or transferred correctly before opening. Moreover, you can use proper encoding if you're dealing with ZIP files created in different languages or systems.
But if your python ZIP file is damaged or won’t open at all, consider using a tool like Repairit File Repair. It carefully handles corrupted ZIP, PDF, Word, Excel, and other file formats with just a simple tap.
Key Features:
- Fix multiple corrupted ZIP files at once. Ideal for Python users processing large datasets or archives.)
- Handles even oversized or complex ZIP files without size limitations.
- After repair, files are automatically decompressed and restored to their original format and structure.
- Lets you view the folder structure and contents of the ZIP before saving.
- Repairs ZIP archives without altering the original data, filenames, or folder structure. This is important for scripts relying on exact file paths.
- Recovers files from ZIPs affected by incomplete downloads, bad sectors, malware, power outages, and system crashes.
- Repairs without modifying the original content. This ensures data integrity for further use in Python workflows.
Repairit File Repair is a great companion tool for Python developers handling damaged ZIP files. Stick with the following repairing instructions:
Step 1: Launch Repairit File Repair and find File Repair under More Types Repair. Click Add to upload your corrupted ZIP python file.

Step 2: Hit Repair so Repairit can scan your zipped ZIP python files and begin fixing any damage.

Step 3: Click Preview to check the repaired ZIP python file version before saving. Press Save or Save All.

Handling Errors or Corrupted ZIP Files
Security Verified. Over 7,302,189 people have downloaded it.
Conclusion
Working with ZIP files in Python is both simple and powerful thanks to the built-in zipfile module. If you need to create, read, or extract ZIP files, Python provides clear, efficient tools for the task. If you need to repair corrupted archives, use tools like Repairit for a smooth Python project flow.
FAQ
Is it possible to password-protect a ZIP file using Python?
The zipfile module supports reading encrypted files, but for creating password-protected ZIPs, use the pyminizip or zipfile with setpassword() (limited support):import pyminizip
pyminizip.compress("file.txt", None, "secure.zip", "password123", 5)
How to generate a Python ZIP file archive with compression?
Use zipfile.ZIP_DEFLATED for compression:with zipfile.ZipFile('compressed.zip', 'w', zipfile.ZIP_DEFLATED) as zipf:
zipf.write('file.txt')
How do I verify if a specific file is in a Python ZIP archive?
Use the in keyword:with zipfile.ZipFile('example.zip', 'r') as zipf:
if 'file1.txt' in zipf.namelist():
print("File exists!")