I was trying to write data into a compressed file using the python gzip module. But the module does not seem to accept the level of compression
I followed the syntax specified in the official Python documentation on gzip
Here is a sample snippet of code, please correct me if I am wrong
import gzip
fd = gzip.GzipFile(filename = "temp", mode = "w", compresslevel = 6)
fd.write("some text")
When I run the file command on the file temp I always get the output as "max compression" even though it is not in this case
file temp
temp: gzip compressed data, was "temp", last modified: Tue Jul 30 23:12:29 2013, max compression
some text
is too small to test. Try with big string.
I tried it with a big text file, and it works as expected.
import gzip
import os
with open('/path/to/big-file', 'rb') as f:
content = f.read()
for level in range(10):
with gzip.GzipFile(filename='temp', mode='w', compresslevel=level) as f:
f.write(content)
print('level={}, size={}'.format(level, os.path.getsize('temp')))
Above code produce following output:
level=0, size=56564
level=1, size=21150
level=2, size=20635
level=3, size=20291
level=4, size=19260
level=5, size=18818
level=6, size=18721
level=7, size=18713
level=8, size=18700
level=9, size=18702
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With