rgk
rgk

Reputation: 866

compress level of python gzip module not working

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

Upvotes: 5

Views: 10308

Answers (2)

falsetru
falsetru

Reputation: 369454

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

Upvotes: 7

DhruvPathak
DhruvPathak

Reputation: 43265

The metadata may be incorrect, but compression level setting does work correctly.

dhruv@dhruv:/tmp$ python z.py <-- level 6
dhruv@dhruv:/tmp$ ll temp 
-rw-rw-r-- 1 dhruv dhruv 215903 Jul 30 23:36 temp
dhruv@dhruv:/tmp$ fg
emacs -nw z.py
dhruv@dhruv:/tmp$ python z.py  <--- level 9 
dhruv@dhruv:/tmp$ ll temp 
-rw-rw-r-- 1 dhruv dhruv 215723 Jul 30 23:36 temp

Contents of z.py:

import gzip
fd = gzip.GzipFile(filename = "temp", mode = "w", compresslevel = 9)
for i in range(0,100000):
    fd.write(str(i))

Upvotes: 0

Related Questions