How to create a zip archive of a directory?
Looking for a speedy zip? shutil.make_archive()
is your Pythonic wand!:
Substitute 'output_filename'
with the preferred zip file name (extension not required), and 'directory_path'
with the directory's path to be compressed and voila, you've got your output_filename.zip
file.
Advanced usage
Low-level control with zipfile
Craving for more control over the compression process? From setting compression levels to including/excluding files, the zipfile
module aids you in bending the rules:
zipfile.ZipFile
gives you an array of options to specify the compression mode and in teamwork with os.path.join
during an os.walk()
, a zipped replica of your directory structure is ready!
Running zipped Python applications
Python 3.5+ has gifted us the capability to bundle and run applications directly from a .pyz
archive using the zipapp
module:
Platform-related nuances for zipping directories
Creating zips on different platforms like Cygwin? A root_dir='.'
protects the consistency of path format across platforms:
Parent directory exclusion in zipping
Keen on creating a zip archive that omits the parent directory of the files?
Zipping Python packages for distribution
When zipping a Python package for distribution, slap an __init__.py
in there and, if your package doubles as an executable, stick a __main__.py
in there too:
Best practices, gotchas, and "Trapzillas"
Make sure to "seal the deal" - Finalize your archive
Don't forget to call zf.close()
to ensure the zip gate is locked, safeguarding your archive content from corruption.
Be aware: File exclusion and symbolic links
News-Flash! Out-of-the-box, zipfile
will ignore symbolic links and will only include regular files in the zip directory. If this is not ideal, gird your loins and get ready to write additional code to handle symbolic links.
Shortcut to creating archives via command-line
Why write a script when you can pop a one-liner command right from your terminal?
Was this article helpful?