pythonpython-2.7gzip

Read a gzip file in Python


When I try to run this code;

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

I get no output on the screen. How to read a gzip file properly?


Solution

  • 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.