Python中使用gzip模块压缩文件的简单教程
发布时间:2020-12-16 19:39:26 所属栏目:Python 来源:网络整理
导读:压缩数据创建gzip文件 先看一个略麻烦的做法 import StringIO,gzipcontent = 'Life is short.I use python'zbuf = StringIO.StringIO()zfile = gzip.GzipFile(mode='wb',compresslevel=9,fileobj=zbuf)zfile.write(content)zfile.close() 但其实有个快捷的封
压缩数据创建gzip文件 import StringIO,gzip content = 'Life is short.I use python' zbuf = StringIO.StringIO() zfile = gzip.GzipFile(mode='wb',compresslevel=9,fileobj=zbuf) zfile.write(content) zfile.close() 但其实有个快捷的封装,不用用到StringIO模块 f = gzip.open('file.gz','wb') f.write(content) f.close() 压缩已经存在的文件 import gzip with open("/path/to/file",'rb') as plain_file: with gzip.open("/path/to/file.gz",'wb') as zip_file: zip_file.writelines(plain_file) 如果不考虑跨平台,只在linux平台,下面这种方式更直接 from subprocess import check_call check_call('gzip /path/to/file',shell=True) (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- 举例详解Python中的split()函数的使用方法
- python – matplotlib savefig中的区分大小写
- Python中的if、else、elif语句用法简明讲解
- 教案生成器(.doc格式)
- python – 为什么重写__contains__会破坏OrderedDict.keys?
- day26_步入百万年薪的第二十六天——tyoe元类与object的联系
- python – 为什么__setitem__比cdef-classes的等效“普通”
- python-3.x – 需要从不同的类调用类方法而不需要初始化第一
- python – 方法返回时,Google App Engine deferred.defer()
- python 【猴子选大王】算法示例