gzip压缩和解压

Example of how to read a compressed file:

import gzip
with gzip.open('file.txt.gz', 'rb') as f:
    file_content = f.read()

Example of how to create a compressed GZIP file:

import gzip
content = "Lots of content here"
with gzip.open('file.txt.gz', 'wb') as f:
    f.write(content)

Example of how to GZIP compress an existing file:

import gzip
import shutil
with open('file.txt', 'rb') as f_in, gzip.open('file.txt.gz', 'wb') as f_out:
    shutil.copyfileobj(f_in, f_out)

参考:

 
posted @ 2019-12-02 11:07  熊猫blue  阅读(837)  评论(0编辑  收藏  举报