How to zip a folder recursively using Python?


It is known that file compression is a routine task in software development and data management in Python. The act of zipping folders recursively is a valuable skill that allows you to compress not only the top-level folder but also all its subdirectories and files; this results in creating a well-organized and space-efficient archive. Python, with its versatility and extensive standard library, makes available multiple approaches to achieve this task effectively.

In this comprehensive and exhaustive article, we will explore various methods of how to zip a folder recursively using Python. We have provided step-by-step explanations and code examples to guide you through the process. If you need to create backups, transfer large volumes of data, or reduce storage space, mastering the art of recursive folder compression will be a powerful asset in your programming journey.

Let's embark on this exciting journey and discover the wonders of zipping folders recursively using Python!

Using shutil.make_archive() for Simple Folder Zipping

Python's "shutil" module offers the "make_archive()" function, which provides a straightforward way to zip a folder recursively.

Example

The "shutil" module in Python provides a simple way to zip a folder recursively using the "make_archive()" function. To undertake this exercise, we first import the "shutil" module, which offers high-level file operations. Next, we define the "zip_folder_with_shutil()" function, allowing us to zip a folder recursively. Within this function, we call "shutil.make_archive()" and provide the "source_folder" and "output_path" to create a zip archive in the "zip" format. By utilizing "make_archive()", the entire folder, along with its subdirectories and files, is compressed, achieving recursive zipping.

import shutil

def zip_folder_with_shutil(source_folder, output_path):
   shutil.make_archive(output_path, 'zip', source_folder)

Utilizing zipfile.ZipFile for Fine-Grained Control

Python's "zipfile" module provides more control over the zipping process, allowing us to specify compression options and handle various aspects of the archive.

Example

For more control over the zipping process, Python's "zipfile" module comes to the rescue. It allows us to specify compression options and manage various aspects of the archive. To begin, we import the "os" module for file and directory operations, along with the "zipfile" module to work with zip archives. Next, we define the "zip_folder_with_zipfile()" function, which enables us to zip a folder recursively with refined control. Inside this function, we use "zipfile.ZipFile" to create a new zip archive at the "output_path" with "w" mode for writing and "zipfile.ZIP_DEFLATED" as the compression method. The "with" statement ensures that the "ZipFile" is properly closed after the block is executed.

Additionally, we employ "os.walk()" to traverse the "source_folder" recursively, processing each file. For each file, we obtain its full path using "os.path.join()" and calculate the relative path with respect to the "source_folder" using "os.path.relpath()". Finally, we add the file to the archive using "zipf.write()", utilizing the calculated relative path as the archive path.

import os
import zipfile

def zip_folder_with_zipfile(source_folder, output_path):
   with zipfile.ZipFile(output_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)
            archive_path = os.path.relpath(file_path, source_folder)
            zipf.write(file_path, archive_path)

Compressing Only Certain Files Based on Conditions

In some scenarios, you may want to include only specific files in the zip archive based on certain conditions. Python's "zipfile" module allows us to filter files and include only those that meet certain criteria.

Example

In specific scenarios, we may need to include only certain files in the zip archive based on specific conditions. Python's "zipfile" module allows us to filter files and include only those that meet these criteria. We continue working with the "os" and "zipfile" modules. The "zip_files_by_condition()" function enables us to achieve this by zipping specific files based on a given condition. Similar to before, we use "zipfile.ZipFile" to create a new zip archive at "output_path" with "w" mode and "zipfile.ZIP_DEFLATED" as the compression method. The "condition_func" argument is a function that evaluates the condition for each file. If the condition is met, the file is included in the archive; otherwise, it is skipped.

import os
import zipfile

def zip_files_by_condition(source_folder, output_path, condition_func):
   with zipfile.ZipFile(output_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)
            if condition_func(file_path):
               archive_path = os.path.relpath(file_path, source_folder)
               zipf.write(file_path, archive_path)

Handling Errors Gracefully with try...except

When working with file operations, errors may occur due to various reasons, such as insufficient permissions or non-existent files. To ensure a robust and user-friendly solution, it's essential to handle such errors gracefully.

Example

Handling errors effectively is crucial when working with file operations. Errors may occur due to various reasons, such as insufficient permissions or non-existent files. To ensure a robust and user-friendly solution, we enhance the "zip_folder_with_zipfile()" function with "try...except" blocks for error handling. The new function, "zip_folder_safely()", allows us to zip a folder recursively while gracefully handling errors. Inside the "with" statement, we wrap the zipping process with a "try...except" block to handle errors that may occur during the zip creation. Additionally, we implement another "try...except" block within the file processing loop to handle specific errors like "FileNotFoundError" for files that may have been removed since the time of traversal.

import os
import zipfile

def zip_folder_safely(source_folder, output_path):
   try:
      with zipfile.ZipFile(output_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)
               try:
                  archive_path = os.path.relpath(file_path, source_folder)
                  zipf.write(file_path, archive_path)
               except FileNotFoundError:
                  print(f"File not found: {file_path}")
               except Exception as e:
                  print(f"An error occurred: {e}")
   except Exception as e:
      print(f"An error occurred while creating the zip archive: {e}")

Using shutil.make_archive() with Different Compression Formats

The "shutil.make_archive()" function supports various compression formats. By choosing the appropriate format, you can tailor the zip archive to your specific needs.

Example

The "shutil.make_archive()" function provides support for various compression formats, allowing us to tailor the zip archive to our specific needs. In this variation of the "zip_folder_with_shutil()" function, we allow the user to specify the compression format as an argument. The "zip_folder_with_different_formats()" function enables us to zip a folder recursively with different compression formats. The "format" argument can take values such as "zip" (default), "tar", "gztar", "bztar", "xztar", and more, depending on the available compression formats supported by your Python environment. By choosing a specific format, you can optimize the archive for compatibility, compression level, and storage space, catering to your requirements effectively.

import shutil

def zip_folder_with_different_formats(source_folder, output_path, format):
   shutil.make_archive(output_path, format, source_folder)

In summary, in this extensive article, we have explored various methods to zip a folder recursively using Python. From the simple "shutil.make_archive()" function to the more advanced options offered by the "zipfile" module, we have learned skills like how to control the zipping process, filter files based on conditions, handle errors gracefully, and choose different compression formats to tailor the zip archive to our needs. By mastering these techniques, you will be equipped with efficient file compression capabilities, empowering you in the process to manage backups, transfer data, and optimize storage effectively. Remember to consider the size of the data, compression formats, and error handling to ensure a smooth and reliable zipping process. It is always better to test your code with different scenarios and edge cases to guarantee its robustness. May your Python adventures continue to thrive as you zip folders recursively with ease and confidence!

Updated on: 22-Aug-2023

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements