67
votes

I've just make excises of gzip on python.

import gzip
f=gzip.open('Onlyfinnaly.log.gz','rb')
file_content=f.read()
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.

2
Try print open('Onlyfinnaly.log.gz', 'rb').read().decode('zlib'). If that doesn't work, can you confirm that the file contains something? - Blender
Yeah, I'm totally sure there is a file whose name is 'Onlyfinally.log'. And what I'm trying to do is to read the content and select some to store another file. But it turn only the blank line on the screen. - Michael
Your code looks correct, but be aware that you are reading the entire file into a string. A more efficient way is usually to read the gzip stream in chunks and process them one at a time. - Krumelur
One of these has a typo. Your q has Onlyfinnaly and your comment has Onlyfinally. The code is otherwise right. - Himanshu

2 Answers

80
votes

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

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

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

import gzip
f=gzip.open('Onlyfinnaly.log.gz','rb')
file_content=f.read()
print file_content

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

51
votes

python: read lines from compressed text files

Using gzip.GzipFile:

import gzip

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