使用 zipfile python 模块创建完整目录的 zip
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我还没有具体测试过,但它与我使用的类似。
此示例来自此处:
http://bitbucket.org/jgrigonis/mathfacts/src /ff57afdf07a1/setupmac.py
I haven't tested this exactly, but it's something similar to what I use.
This example is from here:
http://bitbucket.org/jgrigonis/mathfacts/src/ff57afdf07a1/setupmac.py
我想向此主题添加一个“新”python 2.7 功能:ZipFile 可以用作上下文管理器,因此您可以执行以下操作:
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:
标准的 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.