压缩数据创建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()
登录后复制
但其实有个快捷的封装,不用用到StringIO模块
f = gzip.open('file.gz', 'wb')f.write(content)f.close()
登录后复制
压缩已经存在的文件
python2.7后,可以用with语句
import gzipwith 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_callcheck_call('gzip /path/to/file',shell=True)
登录后复制
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至253000106@qq.com举报,一经查实,本站将立刻删除。
发布者:PHP中文网,转转请注明出处:https://www.chuangxiangniao.com/p/2294048.html