Compress a folder using python
Image Source: Picsum

Key Takeaways

Streamline Python file management by mastering three primary compression methods. This guide covers the high-level simplicity of the shutil module, the granular precision of the zipfile library, and the raw performance of shell-level delegation, providing the technical context needed to select the optimal approach for any automation workflow.

  • Utilize the shutil module’s make_archive and unpack_archive for the most high-level and idiomatic Pythonic approach to folder-level operations.
  • Leverage the zipfile module when granular control over individual file inclusion or metadata within an archive is required, though it necessitates manual recursion for directories.
  • Delegate to system-level binaries via os.system for maximum performance on large datasets, while noting the trade-off in cross-platform portability compared to native Python libraries.
  • Standardize on shutil for projects requiring multi-format support, as it natively handles ZIP, TAR, GZTAR, BZTAR, and XZTAR without additional dependencies.

Using python to compress a folder is a very useful task. In this article, we will see how to compress and decompress a folder using python.

Compress a folder using shutil

import shutil

shutil.make_archive('folder_name', 'zip', 'folder_path')

Here, folder_name is the name of the compressed folder and folder_path is the path of the folder to be compressed. The make_archive() function takes three arguments. The first argument is the name of the compressed folder. The second argument is the format of the compressed folder. The third argument is the path of the folder to be compressed.

Supported formats are: zip, tar, gztar, bztar, xztar

Decompress a folder using shutil

import shutil

shutil.unpack_archive('folder_name.zip', 'folder_path')

Here, folder_name.zip is the name of the compressed folder and folder_path is the path of the folder to be decompress. The unpack_archive() function takes two arguments. The first argument is the name of the compressed folder. The second argument is the path of the folder to be uncompressed.

Supported formats are: zip, tar, gztar, bztar, xztar

Using zipfile module

import zipfile

with zipfile.ZipFile('folder_name.zip', 'w') as zip:
    zip.write('file1')
    zip.write('file2')
    zip.write('file3')

Here, folder_name.zip is the name of the compressed folder. The ZipFile() function takes two arguments. The first argument is the name of the compressed folder. The second argument is the mode of the compressed folder. The write() function takes one argument. The argument is the file to be compressed.

Using python and bash

import os

os.system('zip -r folder_name.zip folder_path')

Here, folder_name.zip is the name of the compressed folder and folder_path is the path of the folder to be compressed. The os.system() function takes one argument. The argument is the command to be executed. The zip command is used to compress a folder. The -r option is used to compress a folder recursively.

Decompress a folder using python and bash

import os

os.system('unzip folder_name.zip')

Here, folder_name.zip is the name of the compressed folder. The unzip command is used to decompress a folder.

Conclusion

In this article, we have seen how to compress and decompress a folder using python. We have seen how to compress a folder using the shutil module, the zipfile module, and python and bash. We have also seen how to decompress a folder using the shutil module and python and bash.

The SQL Whisperer

The SQL Whisperer

Senior Backend Engineer with a deep passion for Ruby on Rails, high-concurrency systems, and database optimization.

Compress the image using python
Prev post

Compress the image using python

Next post

How to download the data from URL using Javascript

How to download the data from URL using Javascript