每天CookBook之Python-079

  • 读写压缩文件
import gzip

with gzip.open('somefile.gz', 'rt') as f:
    text = f.read()

with gzip.open('somefile.gz', 'wt') as f:
    f.write('text')

import bz2

with bz2.open('somefile.bz2', 'rt') as f:
    text = f.read()

with bz2.open('somefile.bz2', 'wt') as f:
    f.write('text')

with gzip.open('somefile.gz', 'wt', compresslevel=5) as f:
    f.write('texts')

f = open('somefile.gz', 'rb')
with gzip.open('f', 'rt') as g:
    text = g.read()
posted @ 2016-07-24 12:54  4Thing  阅读(82)  评论(0编辑  收藏  举报