As a freelancer working with software house companies for several years, I’ve handled a wide range of backend and automation tasks. One recurring scenario I encounter is the need to process ZIP files whether it’s client file uploads, compressed datasets from APIs, or automating deployment workflows.
Recently, a client needed a lightweight, reliable utility to unzip files using Python, and I thought it would be valuable to share how I approached this solution both the simple version and a more feature-rich implementation.
This blog post will cover the basics of unzipping files with Python, walk through an enhanced version of the script, and leave you with practice ideas you can try yourself.
Unzip File Using Python
Let’s start with the simplest form of a script that extracts all contents of a ZIP file.
zipfile import ZipFile
# Open the ZIP file in read mode
with ZipFile('binod.zip', 'r') as zip_object:
# Extract all files to the current directory
zip_object.extractall()
# Print the list of files inside the ZIP
print("Files in ZIP archive:")
print(zip_object.namelist())
Explanation of the Code
Let me break it down:
from zipfile import ZipFile
: We use Python’s built-inzipfile
module – no need to install anything extra.ZipFile('binod.zip', 'r')
: This opens the filebinod.zip
in read mode.zip_object.extractall()
: This line extracts everything inside the archive to the current directory.zip_object.namelist()
: Finally, we print out the names of the files inside the ZIP to verify the contents.
This works perfectly for simple tasks, but in real projects, we often need more control and better error handling.
Enhance Version
In a production scenario, I needed more robust functionality:
- What if the ZIP file doesn’t exist?
- What if I only want to extract certain files?
- Where should the extracted files go?
Here’s an improved version of the code:
import os
from zipfile import ZipFile
zip_path = 'binod.zip'
extract_path = 'unzipped_files'
# Check if the zip file exists
if os.path.exists(zip_path):
with ZipFile(zip_path, 'r') as zip_object:
# List files in the zip
file_list = zip_object.namelist()
print("Files in the ZIP archive:")
for file in file_list:
print(f" - {file}")
# Create a custom extraction folder if not exists
os.makedirs(extract_path, exist_ok=True)
# Extract all files
zip_object.extractall(path=extract_path)
print(f"\nAll files extracted to: {extract_path}")
# Optionally extract specific file(s)
specific_file = 'clcoding.pdf'
if specific_file in file_list:
zip_object.extract(specific_file, path='single_file_output')
print(f"\n'{specific_file}' extracted to 'single_file_output/'")
else:
print(f"\n'{specific_file}' not found in ZIP.")
else:
print(f"Error: ZIP file '{zip_path}' not found.")
What’s New in This Version
- File existence check: Prevents the script from crashing if the ZIP file is missing.
- Custom extraction path: Files are extracted into a dedicated folder (
unzipped_files
), keeping things organized. - Selective file extraction: I extracted just one file (
clcoding.pdf
) into a separate folder – handy for targeted processing. - Logging and feedback: Each step gives clear console feedback – critical for debugging.
Explain Code
To help our junior developers (and even myself during testing), I created a few small challenges based on this project:
- Filter by extension
Only extract files that end with.jpg
or.pdf
. - Use
infolist()
Get file size, compression type, and other metadata. - Build a CLI tool with
argparse
Let users pass in the ZIP filename and output directory via command line. - Progress bar with
tqdm
Show file-by-file extraction progress for large ZIPs.
Final Thought
Working with ZIP files is a common task in many backend and automation workflows. While Python makes it easy to handle these files out of the box, adding just a bit more logic can turn a simple script into a flexible, production ready tool.
Whether you’re automating data pipelines, building client-facing utilities, or integrating file handling into larger systems, this approach is both reliable and easy to scale.