How to read a specific line from a gzip file with python

805 Views Asked by At

I have a big gzip file (11GB) and I want to print as fast as possible the line that I want with Python. I have tried to do it with linecache.getline(), but as the own function open the file, you are not able to open it with gzip.

1

There are 1 best solutions below

0
BoarGules On

linecache expects to get a textfile. A file that has been compressed using gzip is not a textfile. To do what you want requires two steps. (1) Unzip the file so that you have a textfile. (2) Use linecache on the textfile. You can do both of those things in Python, but only one after the other.

I understand that you want to get at a specific line without having to decompress then entire zipfile. But that is not how zipfile compression works. There is unlikely to be anything in the compressed data that corresponds to the notion of a line of text.