all Python 3shutilfunction
shutil.make_archive()
archives
Packs a directory into a zip or tar archive (gz, bz2, xz, zst) in one line and returns the name of the created file.
Syntax
shutil.make_archive(base_name, format, root_dir=None, base_dir=None, verbose=0, dry_run=0, owner=None, group=None, logger=None) -> str
base_name is given without an extension — the function adds it: make_archive('backup.zip', 'zip', ...) creates backup.zip.zip. root_dir is the directory that paths inside the archive are relative to; without it the current directory is archived, and the archive may end up containing itself. The returned path is absolute only when root_dir is given; without it, it follows base_name as passed, usually relative. Before 3.10.6 the function changed the process's working directory and was unsafe with threads.
Examples
import os, shutil, zipfile
os.makedirs('data/sub')
open('data/sub/f.txt', 'w').close()
name = shutil.make_archive('backup', 'zip', root_dir='data')
print(os.path.basename(name)) # → backup.zip
print(zipfile.ZipFile(name).namelist()) # → ['sub/', 'sub/f.txt']
import os, shutil
os.mkdir('data')
name = shutil.make_archive('backup.zip', 'zip', root_dir='data')
print(os.path.basename(name)) # → backup.zip.zip
See also
Open in the full glossarydocs.python.orgReport an inaccuracy