Read from a gzip file in python Read from a gzip file in python python python

Read from a gzip file in python


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

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

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

import gzipf=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.


python: read lines from compressed text files

Using gzip.GzipFile:

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


If you want to read the contents to a string, then open the file in text mode (mode="rt")

import gzipwith gzip.open("Onlyfinnaly.log.gz", mode="rt") as f:    file_content = f.read()    print(file_content)