bz2 or gzip compression

Read and write compressed files

bz2 compression (Python 3)

import bz2

# read
with bz2.open('input_file.bz2', 'rt', encoding='utf-8') as f:
    for line in f:
        l=line.strip()
        print(l)

# write
with bz2.open('output_file.bz2', 'w') as f:
    f.write('Hello world\n')

gzip compression (Python 3)

import gzip

# read
with gzip.open('input_file.gz', 'rt', encoding='utf-8') as f:
    for line in f:
        l=line.strip()
        print(l)

# write
with gzip.open('output_file.gz', 'w') as f:
    f.write('Hello world\n')


Python 2.7


bz2.BZ2File('input_file.bz2', 'r')

gzip.open('output_file.gz', 'rt')



Compress text file using gzip

# read original file, write as gz compressed file
import gzip
import shutil
with open('path/to/input/file.txt', 'rb') as f_in:               # open original file for reading
    with gzip.open('path/to/output/file.txt.gz', 'wb') as f_out: # open gz file for writing
        shutil.copyfileobj(f_in, f_out)                          # write/copy text file into gz file