使用 zipfile python 模块创建完整目录的 zip

发布于 2024-10-08 20:53:28 字数 246 浏览 3 评论 0原文

zip = zipfile.ZipFile(destination+ff_name,"w")
            zip.write(source)
            zip.close()

上面是我正在使用的代码,这里的“source”是目录的路径。但是当我运行此代码时,它只是压缩源文件夹,而不是其中包含的文件和文件夹。我希望它递归地压缩源文件夹。使用 tarfile 模块我可以做到这一点而无需传递任何附加信息。

zip = zipfile.ZipFile(destination+ff_name,"w")
            zip.write(source)
            zip.close()

Above is the code that I am using, and here "source" is the path of the directory. But when I run this code it just zips the source folder and not the files and and folders contained in it. I want it to compress the source folder recursively. Using tarfile module I can do this without passing any additional information.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

ぶ宁プ宁ぶ 2024-10-15 20:53:29

我还没有具体测试过,但它与我使用的类似。

zip = zipfile.ZipFile(destination+ff_name, 'w', zipfile.ZIP_DEFLATED)
rootlen = len(source) + 1
for base, dirs, files in os.walk(source):
    for file in files:
        fn = os.path.join(base, file)
        zip.write(fn, fn[rootlen:])

此示例来自此处:

http://bitbucket.org/jgrigonis/mathfacts/src /ff57afdf07a1/setupmac.py

I haven't tested this exactly, but it's something similar to what I use.

zip = zipfile.ZipFile(destination+ff_name, 'w', zipfile.ZIP_DEFLATED)
rootlen = len(source) + 1
for base, dirs, files in os.walk(source):
    for file in files:
        fn = os.path.join(base, file)
        zip.write(fn, fn[rootlen:])

This example is from here:

http://bitbucket.org/jgrigonis/mathfacts/src/ff57afdf07a1/setupmac.py

巾帼英雄 2024-10-15 20:53:29

我想向此主题添加一个“新”python 2.7 功能:ZipFile 可以用作上下文管理器,因此您可以执行以下操作:

        with zipfile.ZipFile(my_file, 'w') as myzip:
            rootlen = len(xxx) #use the sub-part of path which you want to keep in your zip file 
            for base, dirs, files in os.walk(pfad):
                for ifile in files:
                    fn = os.path.join(base, ifile)
                    myzip.write(fn, fn[rootlen:])

I'd like to add a "new" python 2.7 feature to this topic: ZipFile can be used as a context manager and therefore you can do things like this:

        with zipfile.ZipFile(my_file, 'w') as myzip:
            rootlen = len(xxx) #use the sub-part of path which you want to keep in your zip file 
            for base, dirs, files in os.walk(pfad):
                for ifile in files:
                    fn = os.path.join(base, ifile)
                    myzip.write(fn, fn[rootlen:])
兰花执着 2024-10-15 20:53:28

标准的 os.path.walk() 函数将可能对此很有用。

或者,阅读 tarfile 模块以了解它如何工作肯定会有所帮助。事实上,了解标准库的各个部分是如何编写的是我学习 Python 的一个非常宝贵的部分。

The standard os.path.walk() function will likely be of great use for this.

Alternatively, reading the tarfile module to see how it does its work will certainly be of benefit. Indeed, looking at how pieces of the standard library were written was an invaluable part of my learning Python.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文