Last modified: Feb 10, 2026 By Alexander Williams
Python Zip Directory: Compress Files Easily
Working with files is a common task in programming.
You often need to bundle multiple files together. This is where compression becomes essential.
Python provides a powerful built-in module for this purpose. It is called zipfile.
This guide will show you how to zip an entire directory. You will learn to preserve its structure.
Understanding the zipfile Module
Python's zipfile module is part of the standard library. You do not need to install it.
It allows you to create, read, write, and extract ZIP archives. It handles both compression and decompression.
For zipping directories, we focus on the ZipFile class. Its write method is key.
This is different from the built-in zip function for iterating. That function combines sequences.
You can learn more about that in our guide on the Python Zip Function Guide: Iterate in Parallel.
How to Zip a Single Directory
Let's start with a simple example. We will compress all files in a folder.
We use the os.walk() function to traverse the directory tree. This finds all files and subdirectories.
The crucial step is to add each file to the archive with its correct path.
import os
import zipfile
def zip_directory(directory_path, output_zip_path):
"""
Zips the contents of a directory into a single ZIP file.
"""
# Create a ZipFile object in write mode
with zipfile.ZipFile(output_zip_path, 'w', zipfile.ZIP_DEFLATED) as zipf:
# Walk through the directory
for root, dirs, files in os.walk(directory_path):
for file in files:
# Create the full file path
file_path = os.path.join(root, file)
# Create the archive name (path relative to the directory being zipped)
arcname = os.path.relpath(file_path, start=directory_path)
# Write the file to the zip
zipf.write(file_path, arcname)
print(f"Successfully created {output_zip_path}")
# Example usage
zip_directory('./my_project', './project_backup.zip')
This script creates a ZIP file named 'project_backup.zip'. It contains all files from 'my_project'.
The ZIP_DEFLATED constant enables compression. Using 'w' mode creates a new archive.
The arcname parameter is vital. It stores files with their relative paths inside the ZIP.
This preserves the folder structure when you extract it later.
Including or Excluding Specific Files
You might not want to zip every file. You can filter by file extension or name.
Here is an example that only zips Python (.py) and text (.txt) files.
def zip_filtered(directory_path, output_zip_path, extensions=('.py', '.txt')):
"""
Zips only files with specific extensions.
"""
with zipfile.ZipFile(output_zip_path, 'w', zipfile.ZIP_DEFLATED) as zipf:
for root, dirs, files in os.walk(directory_path):
for file in files:
if file.endswith(extensions):
file_path = os.path.join(root, file)
arcname = os.path.relpath(file_path, start=directory_path)
zipf.write(file_path, arcname)
print(f"Created filtered archive: {output_zip_path}")
# Zip only Python and text files
zip_filtered('./my_project', './project_code.zip')
This control is useful for creating lean archives. You avoid temporary files or large binaries.
For more on reading and extracting from such archives, see Python Zip Files: Create, Read, Extract Archives.
Zipping Multiple Directories into One Archive
Sometimes you need to combine several folders. The process is similar but requires multiple source paths.
You must manage the archive paths carefully to avoid name collisions.
def zip_multiple_directories(directory_list, output_zip_path):
"""
Zips multiple directories into a single ZIP file.
Each directory becomes a top-level folder in the archive.
"""
with zipfile.ZipFile(output_zip_path, 'w', zipfile.ZIP_DEFLATED) as zipf:
for base_dir in directory_list:
for root, dirs, files in os.walk(base_dir):
for file in files:
file_path = os.path.join(root, file)
# Include the base directory name in the archive path
parent_name = os.path.basename(base_dir)
rel_path = os.path.relpath(file_path, start=base_dir)
arcname = os.path.join(parent_name, rel_path)
zipf.write(file_path, arcname)
print(f"Combined archive created: {output_zip_path}")
# Zip two different project folders
folders = ['./project_a', './project_b']
zip_multiple_directories(folders, './all_projects.zip')
In the resulting ZIP, files will be under 'project_a/' and 'project_b/' folders. This keeps them organized.
This technique is great for bundling related projects for distribution or backup.
Practical Example: Backing Up Log Files
Let's apply this to a real-world scenario. We will back up a week's worth of application log files.
We assume log files are named with dates, like 'app_2023_10_27.log'.
import datetime
def backup_recent_logs(log_directory, days=7):
"""
Creates a ZIP backup of log files modified in the last 'days' days.
"""
cutoff_date = datetime.datetime.now() - datetime.timedelta(days=days)
backup_name = f"logs_backup_{datetime.datetime.now().strftime('%Y%m%d')}.zip"
backup_path = os.path.join(log_directory, backup_name)
with zipfile.ZipFile(backup_path, 'w', zipfile.ZIP_DEFLATED) as zipf:
for root, dirs, files in os.walk(log_directory):
for file in files:
if file.endswith('.log'):
file_path = os.path.join(root, file)
# Check the file's modification time
mod_time = datetime.datetime.fromtimestamp(os.path.getmtime(file_path))
if mod_time > cutoff_date:
arcname = os.path.relpath(file_path, start=log_directory)
zipf.write(file_path, arcname)
print(f"Log backup created: {backup_path}")
return backup_path
# Backup logs from the last 7 days
backup_recent_logs('./application_logs')
This script is efficient and automated. You can run it as a scheduled task.
It demonstrates filtering by both file type and modification date.
Common Pitfalls and Best Practices
Zipping directories is straightforward. But you should be aware of common issues.
Path Errors: Always check if the source directory exists. Use os.path.exists().
Large Files: The zipfile module can handle large files. But it loads file data into memory.
For huge files, process them in chunks if needed.
Compression Level:ZIP_DEFLATED offers good compression. ZIP_STORED adds files without compression.
Use stored mode for already compressed files like JPG or MP4.
Windows Paths: Be careful with backslashes. Using os.path.join ensures cross-platform compatibility.
Remember, the zipfile module is for ZIP archives. For simply combining lists, you need the zip function.
Learn how to use it in our article on Python Zip Two Lists.
Conclusion
Zipping directories in Python is a powerful skill. The zipfile module makes it simple.
You can compress single folders, multiple directories, and apply filters.
This is ideal for backups, data distribution, and organizing project files.
Always preserve the directory structure with relative paths. This ensures clean extraction.
Start automating your file management tasks with Python today. It saves time and disk space.