python zipfile.ZipFile()方法从6M原始文件生成20G zip文件
我正在运行一个 python 程序(v2.7),它会压缩输出,以便可以通过电子邮件发送。
通常这会按预期工作,但有时压缩文件太大,导致机器磁盘空间不足。然而,当我使用查找器手动压缩文件时,它工作正常。
在本例中,使用 finder 将 6MB 文件压缩为 1.6MB 文件,但 python zip 方法生成了 20GB 文件。这是发生压缩的代码:
zip = zipfile.ZipFile(zipfilename,"w",zipfile.ZIP_DEFLATED)
for f in os.listdir("."):
if fnmatch.fnmatch(f,"*final*"):
zip.write(f)
zip.close()
有没有办法解决这个问题或至少避免生成巨大的文件?
I am running a python program (v2.7) which zips output so that it can be emailed.
Usually this works as expected, but occasionally the zipped file is so huge that the machine runs out of disk space. Yet when I zip the file manually using the finder, it works fine.
In this case, the 6MB file gets zipped down to a 1.6MB file using the finder, but the python zip method generated a 20GB file. Here is the code where the zipping is happening:
zip = zipfile.ZipFile(zipfilename,"w",zipfile.ZIP_DEFLATED)
for f in os.listdir("."):
if fnmatch.fnmatch(f,"*final*"):
zip.write(f)
zip.close()
Is there a way to fix this or at least avoid generating a gigantic file?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您是否可能在同一目录中创建该 zip 文件,然后程序尝试将 zip 文件本身添加到 zip 文件中?
Do you maybe create that zip file in the same directory and the program is then trying to add the zipfile itself to the zip file?
它是Linux?,我认为你包括隐藏的文件和文件夹?
Its Linux?, i think you are including hidden files and folders?