When you want to compress an entire directory (folder) in Python, using the make_archive() function from the standard library shutil module is overwhelmingly easier than writing loop processes with the zipfile or tarfile modules.
This function allows you to archive an entire directory into a specified format (such as ZIP or TAR) with just a single line of code.
List of Compression Formats
The following table shows the format strings you can specify for the second argument of make_archive, along with the corresponding generated file types.
| Format String | Generated Format / Extension | Note |
"zip" | ZIP file (.zip) | Common. Can be extracted by standard Windows features. |
"tar" | Tar file (.tar) | Archive without compression. |
"gztar" | gzip compressed tar (.tar.gz) | Standard on Linux/Mac. Good balance of speed and compression. |
"bztar" | bzip2 compressed tar (.tar.bz2) | Higher compression than gzip, but slightly slower. |
"xztar" | xz compressed tar (.tar.xz) | Boasts very high compression rates, but takes time to process. |
Implementation Example: Batch Compression of Website Assets
In this scenario, we will compress a directory named website_assets, which contains website images and CSS, into a ZIP file named release_package.zip for distribution.
Source Code
import shutil
import os
# 1. Path of the directory (folder) to compress
# Example: "website_assets" folder in the current directory
target_directory = "website_assets"
# Create a dummy directory for this example (for verification)
if not os.path.exists(target_directory):
os.makedirs(target_directory)
with open(f"{target_directory}/style.css", "w") as f:
f.write("body { color: #333; }")
with open(f"{target_directory}/logo.png", "w") as f:
f.write("dummy image data")
# 2. Output filename (The extension is added automatically, so it is not needed here)
output_filename = "release_package"
# 3. Execute shutil.make_archive(output_name, format, target_directory)
# Specify the "directory to compress" in the root_dir argument
archive_path = shutil.make_archive(output_filename, "zip", root_dir=target_directory)
print(f"Compression complete: {archive_path}")
Execution Result
Compression complete: /path/to/your/directory/release_package.zip
Explanation
Meaning of Arguments
- 1st Argument (
base_name): The filename (path) of the archive to be created. The extension is automatically added according to the specified format (e.g.,"release_package"becomesrelease_package.zip). - 2nd Argument (
format): Specify one of the format strings from the table above. root_dir: Specify the root directory to include in the archive. The contents of this directory will be expanded directly under the root of the archive.
Important Note
While shutil.make_archive is convenient, if you compress a huge directory, the program will block (stop responding) until the process is complete because it uses memory and temporary space during processing.
If you need to display a progress bar or require detailed exclusion settings, you need to implement the process individually using modules like zipfile.
