亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Python中使用gzip模塊壓縮文件的簡單教程

 更新時間:2015年04月08日 09:47:50   投稿:goldensun  
這篇文章主要介紹了Python中使用gzip模塊壓縮文件的簡單教程,本文的例子主要針對類UNIXZ系統(tǒng),需要的朋友可以參考下

壓縮數(shù)據(jù)創(chuàng)建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()

壓縮已經(jīng)存在的文件
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)

相關(guān)文章

最新評論