How do I create a zip file of a file path using Python, including empty directories?

后端 未结 4 1891
灰色年华
灰色年华 2020-12-30 14:55

I\'ve been trying to use the zipfile and shutil.make_archive modules to recursively create a zip file of a directory. Both modules work great--exc

4条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-30 15:05

    You'll need to register a new archive format to do that, since the default ZIP archiver does not support that. Take a look at the meat of the existing ZIP archiver. Make your own archiver that creates directories using that currently-unused dirpath variable. I looked for how to create an empty directory and found this:

    zip.writestr(zipfile.ZipInfo('empty/'), '')
    

    With that, you should be able to write the necessary code to make it archive empty directories.

提交回复
热议问题