Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

compress level of python gzip module not working

Tags:

python

gzip

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
like image 303
rgk Avatar asked Jul 30 '13 17:07

rgk


1 Answers

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
like image 144
falsetru Avatar answered Oct 30 '22 13:10

falsetru