Skip to main content

Command Palette

Search for a command to run...

How to Compress Backups with Python

Published
3 min read
How to Compress Backups with Python

Introduction

When creating backups, one key improvement you can make is to compress your folders into .zip files. This saves space and makes it easier to transfer or archive your backups — whether you're storing them locally or uploading them to the cloud.

This article explains how to do it in Python using the built-in zipfile module.

Why Compress Your Backups?

Here’s why compression is useful in a backup process:

BenefitDescription
Save Disk SpaceCompressing reduces file size — especially for text-heavy folders.
Easy to ArchiveYou only deal with a single file per backup.
Cloud-ReadyCompressed files upload faster and more cleanly to services like Google Drive, Dropbox, or S3.
Clean OrganizationTimestamped .zip files are easier to manage than entire folder trees.

Tools You’ll Use

Python has a built-in module called zipfile that supports compression out of the box. No installation is needed.

We'll also use:

  • os for navigating the filesystem

  • datetime to generate a timestamp

  • shutil (optional) if you want to delete the original folder after zipping

Step-by-Step Script: Compress Folder into a Zip File

Here’s a complete example with step-by-step explanation:

import os
import zipfile
from datetime import datetime

# Step 1: Set the source folder and destination path
source_folder = r"C:\Users\YourName\Documents\Project"  # Folder to back up
backup_dir = r"C:\Users\YourName\Backups"               # Where to store compressed backups

# Create backup directory if it doesn't exist
os.makedirs(backup_dir, exist_ok=True)

# Step 2: Create a timestamped zip file name
timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
zip_filename = f"backup_{timestamp}.zip"
zip_path = os.path.join(backup_dir, zip_filename)

# Step 3: Compress the folder
with zipfile.ZipFile(zip_path, 'w', zipfile.ZIP_DEFLATED) as zipf:
    for root, dirs, files in os.walk(source_folder):
        for file in files:
            file_path = os.path.join(root, file)
            # Arcname keeps the folder structure inside the zip
            arcname = os.path.relpath(file_path, start=source_folder)
            zipf.write(file_path, arcname)

print(f"Backup completed and saved to: {zip_path}")

What's Happening in the Script?

1. Set Paths

source_folder = r"C:\..."
backup_dir = r"C:\..."

These variables tell Python where to find the folder to back up and where to save the zipped file.

2. Create a Timestamped Filename

timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")

This generates a unique filename like backup_20250513_103500.zip, so backups don't overwrite each other.

3. Create the ZIP File

with zipfile.ZipFile(zip_path, 'w', zipfile.ZIP_DEFLATED) as zipf:

This opens a new .zip file in write mode and enables compression using the ZIP_DEFLATED algorithm.

4. Walk Through All Files and Add Them

for root, dirs, files in os.walk(source_folder):
    for file in files:
        file_path = os.path.join(root, file)
        arcname = os.path.relpath(file_path, start=source_folder)
        zipf.write(file_path, arcname)
  • os.walk() walks through every subfolder and file.

  • os.path.relpath() ensures that the folder structure is preserved in the .zip.

  • zipf.write() adds each file to the archive.

Optional: Delete Original Folder After Compression

If you want to clean up after compression:

import shutil
shutil.rmtree(source_folder)  # WARNING: This deletes the folder permanently!

Only do this if you're 100% sure the zip was created correctly.

Testing Tips

Before you automate this process (e.g., with Task Scheduler or cron), test your script:

  • Does it zip the entire folder?

  • Is the file size significantly smaller?

  • Can you extract it and verify contents?

Summary

With a few lines of Python, you can:

  • Compress your backups

  • Save disk space

  • Stay organized

  • Prepare your files for cloud storage or email delivery

This is an essential step in any robust, automated backup system.

Happy Scripting ! !

More from this blog

PyScript Academy

29 posts

PyScript Academy is a blog sharing practical Python scripts, tips, and mini projects—helping you learn Python by doing, one useful script at a time.