首页
学习
活动
专区
圈层
工具
发布

python 读写压缩文件

gzip 读写 gz

bz2 读写 bz2

gzip compression

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

bz2 compression

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

gzip compression

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

bz2 compression

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

compresslevel

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

下一篇
举报
领券