There is a module gzip. Usage:
Example of how to create a compressed GZIP file:
import gzip
content = b"Lots of content here"
f = gzip.open('/home/joe/file.txt.gz', 'wb')
f.write(content)
f.close()
Example of how to GZIP compress an existing file:
import gzip
f_in = open('/home/joe/file.txt')
f_out = gzip.open('/home/joe/file.txt.gz', 'wb')
f_out.writelines(f_in)
f_out.close()
f_in.close()
EDIT:
Jace Browning's answer using with
in Python >= 2.7 is obviously more terse and readable, so my second snippet would (and should) look like:
import gzip
with open('/home/joe/file.txt', 'rb') as f_in, gzip.open('/home/joe/file.txt.gz', 'wb') as f_out:
f_out.writelines(f_in)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…