飘逸的python - 简明gzip模块压缩教程
飘逸的python - 简明gzip模块压缩教程分类: Python2013-07-2207:55 109人阅读 评论(0) 收藏 举报gzip目录(?)[+]压缩数据创建gzip文件先看一个略麻烦的做法[python] viewplaincopyimport StringIO,gzip
·
压缩数据创建gzip文件
先看一个略麻烦的做法
但其实有个快捷的封装,不用用到StringIO模块
- 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模块
python2.7后,可以用with语句
- 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)
更多推荐
所有评论(0)