如何在Python中压缩大文件?
我遇到的问题是存储文件的名称.存储的文件未使用原始/未压缩文件名命名.而是使用存档名称(附加的“.gz”扩展名)命名存储的文件.
预期结果: 实际结果: 阅读gzip文档(https://docs.python.org/2.7/library/gzip.html)示例代码: import gzip import shutil with 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号:
试试这个: import gzip import shutil with 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) (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |