How to create a zip archive of a directory in Python?

Martha Yi picture Martha Yi · Dec 6, 2009 · Viewed 401.7k times · Source

How can I create a zip archive of a directory structure in Python?

Answer

crdavis picture crdavis · Sep 3, 2014

The easiest way is to use shutil.make_archive. It supports both zip and tar formats.

import shutil
shutil.make_archive(output_filename, 'zip', dir_name)

If you need to do something more complicated than zipping the whole directory (such as skipping certain files), then you'll need to dig into the zipfile module as others have suggested.