预期结果:
file.txt.gz {存档名称}
…. file.txt {存储文件名}
实际结果:
file.txt.gz {存档名称}
…. file.txt.gz {存储文件名}
阅读gzip文档(https://docs.python.org/2.7/library/gzip.html)示例代码:
import gzipimport shutilwith open('file.txt','rb') as f_in,gzip.open('file.txt.gz','wb') as f_out: shutil.copyfileobj(f_in,f_out)
如何获取存档以存储名为“file.txt”而不是“file.txt.gz”的文件?
解决方法 你必须使用gzip.Gzipfile();简写gzip.open()不会做你想要的.第the doc号:
When fileobj is not
None
,the filename argument is only used to be included in the gzip file header,which may include the original filename of the uncompressed file. It defaults to the filename of fileobj,if discernible; otherwise,it defaults to the empty string,and in this case the original filename is not included in the header.
试试这个:
import gzipimport shutilwith open('file.txt','rb') as f_in: with open('file.txt.gz','wb') as f_out: with gzip.Gzipfile('file.txt','wb',fileobj=f_out) as f_out: shutil.copyfileobj(f_in,f_out)总结
以上是内存溢出为你收集整理的如何在Python中压缩大文件?全部内容,希望文章能够帮你解决如何在Python中压缩大文件?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)