Read and write compressed or uncompressed files (Python3)
compression is identified by file ending:
uncompressed: filename.txt
gzip: filename.txt.gz
bzip2: filename.txt.bz2
gzip compression
import fileinput
# read gzip file
with fileinput.hook_compressed('input_file.gz','rt') as f:
for line in f:
l=line.strip()
print(l)
# write gzip file (byte mode required)
with fileinput.hook_compressed('output_file.gz','wb') as f:
f.write( b'Number of samples: ' )
f.write( str(50).encode('utf-8') )
f.write( b'\n')
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
https://docs.python.org/3/library/fileinput.html#fileinput.hook_compressed