簡明python教程pdf,飄逸的python - 簡明gzip模塊壓縮教程

 2023-10-07 阅读 30 评论 0

摘要:壓縮數據創建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) z

壓縮數據創建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()

壓縮已經存在的文件

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)


?

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/4/123961.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息