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

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

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

25条回答
  •  悲&欢浪女
    2020-11-22 08:03

    Say you want to Zip all the folders(sub directories) in the current directory.

    for root, dirs, files in os.walk("."):
        for sub_dir in dirs:
            zip_you_want = sub_dir+".zip"
            zip_process = zipfile.ZipFile(zip_you_want, "w", zipfile.ZIP_DEFLATED)
            zip_process.write(file_you_want_to_include)
            zip_process.close()
    
            print("Successfully zipped directory: {sub_dir}".format(sub_dir=sub_dir))
    

提交回复
热议问题