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

后端 未结 25 2997
暗喜
暗喜 2020-11-22 07:12

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

25条回答
  •  Happy的楠姐
    2020-11-22 08:00

    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.

提交回复
热议问题