This shows you how to compress and decompress a typical text file using Python module gzip ...
# using module gzip to compress and decompress files
# default compression level is 9 (out of 1-9)
# needs Python27 or higher
# tested with Python27 and Python32 by vegaseat
import gzip
txt_file = "Edictionary.txt"
gz_file = "Edictionary.gz"
# reads in a 747kb .txt file, compresses to a 201kb .gz file
# mode='rb' is needed for the text file
with open(txt_file, 'rb') as f_in:
with gzip.open(gz_file, 'wb') as f_out:
f_out.writelines(f_in)
print("Read .gz file in as one string ..")
with gzip.open(gz_file, 'rb') as f:
# decode <class 'bytes'> to string
# .decode("utf8") does not work here
text2 = f.read().decode("latin1")
print("Show first 80 characters:")
print(text2[:80])
print('-'*37)
# a mildly different approach using the class
print("Read .gz file in as list of lines ...")
gzin = gzip.GzipFile(gz_file, 'rb')
lines = gzin.readlines()
gzin.close()
print("Show first 10 lines:")
for line in lines[:10]:
# decode <class 'bytes'> to string
line = line.decode("latin1").rstrip()
print(line)
'''result ...
Read .gz file in as one string ..
Show first 80 characters:
Aachen
aardvark
aardvarks
aaron
abaci
aback
abacus
abacuses
abaft
abalo
-------------------------------------
Read .gz file in as list of lines ...
Show first 10 lines:
Aachen
aardvark
aardvarks
aaron
abaci
aback
abacus
abacuses
abaft
abalone
'''