加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

如何在Python中压缩大文件?

发布时间:2020-12-20 13:12:42 所属栏目:Python 来源:网络整理
导读:我遇到的问题是存储文件的名称.存储的文件未使用原始/未压缩文件名命名.而是使用存档名称(附加的“.gz”扩展名)命名存储的文件. 预期结果: file.txt.gz {存档名称} …. file.txt {存储文件名} 实际结果: file.txt.gz {存档名称} …. file.txt.gz {存储文件
我遇到的问题是存储文件的名称.存储的文件未使用原始/未压缩文件名命名.而是使用存档名称(附加的“.gz”扩展名)命名存储的文件.

预期结果:
file.txt.gz {存档名称}
…. file.txt {存储文件名}

实际结果:
file.txt.gz {存档名称}
…. file.txt.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号:

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 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)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读