Compressing a directory (folder) into a zip or tar in Python

Money and Business

When compressing an entire directory (folder) into a zip file in Python, you can use os.scandir() or os.listdir() to create a list of files and use the zipfile module to compress them, but it is easier to use the make_archive () of the shutil module is easier.

In addition to zip, other formats such as tar are also supported.

For more information on compressing and uncompressing zip files using the zipfile module, please refer to the following article.

Compress a directory (folder) into a zip file:shutil.make_archive()

The first argument, base_name, specifies the name of the zip file to be created (without extension), and the second argument, format, specifies the archive format.

The following can be selected for the argument format.

  • 'zip'
  • 'tar'
  • 'gztar'
  • 'bztar'
  • 'xztar'

The third argument, root_dir, specifies the path of the root directory of the directory to be compressed, and the fourth argument, base_dir, specifies the path of the directory to be compressed relative to the root_dir. Both are set to the current directory by default.

If base_dir is omitted, the entire root_dir will be compressed.

data/temp
For example, suppose we have a directory with the following structure.

dir
├── dir_sub
   └── test_sub.txt
└── test.txt
import shutil

shutil.make_archive('data/temp/new_shutil', 'zip', root_dir='data/temp/dir')

The new_shutil.zip compressed with the above settings omitting the base_dir will be decompressed as follows.

new_shutil
├── dir_sub
   └── test_sub.txt
└── test.txt

Then, if the directory in root_dir is specified for base_dir, the following will be shown.

shutil.make_archive('data/temp/new_shutil_sub', 'zip', root_dir='data/temp/dir', base_dir='dir_sub')

The new_shutil_sub.zip compressed with the above settings will be decompressed as follows.

dir_sub
└── test_sub.txt
Copied title and URL