Python中使用gzip模块压缩文件的简单教程

来源:未知 浏览 132次 时间 2021-06-03 17:26

压缩数据创建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文件
先看一个略麻烦的做法

Python中使用gzip模块压缩文件的简单教程

f = gzip.open('file.gz', 'wb')f.write(content)f.close()

压缩已经存在的文件
python2.7后网站转化率不用用到StringIO模块

f = gzip.open('file.gz', 'wb')f.write(content)f.close()

压缩已经存在的文件
python2.7后可以用with语句

Python中使用gzip模块压缩文件的简单教程

from subprocess import check_callcheck_call('gzip /path/to/file',shell=True)

标签: importpathfileto