Read from a gzip file in python


I've just make excises of gzip on python.

import gzip'Onlyfinnaly.log.gz','rb')
print file_content

And I get no output on the screen. As a beginner of python, I'm wondering what should I do if I want to read the content of the file in the gzip file. Thank you.

This question is tagged with python python-2.7 gzip

~ Asked on 2012-10-15 19:21:25

The Best Answer is


Try gzipping some data through the gzip libary like this...

import gzip
content = "Lots of content here"
f ='Onlyfinnaly.log.gz', 'wb')

... then run your code as posted ...

import gzip'Onlyfinnaly.log.gz','rb')
print file_content

This method worked for me as for some reason the gzip library fails to read some files.

~ Answered on 2012-10-26 08:22:11


python: read lines from compressed text files

Using gzip.GzipFile:

import gzip

with'input.gz','r') as fin:        
    for line in fin:        
        print('got line', line)

~ Answered on 2016-01-01 07:21:21

Most Viewed Questions: