Site icon FSIBLOG

How to Extract Unzip Files with Python

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:

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:

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

Explain Code

To help our junior developers (and even myself during testing), I created a few small challenges based on this project:

  1. Filter by extension
    Only extract files that end with .jpg or .pdf.
  2. Use infolist()
    Get file size, compression type, and other metadata.
  3. Build a CLI tool with argparse
    Let users pass in the ZIP filename and output directory via command line.
  4. 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.

Exit mobile version