Search code examples
pythoncompressiongziplarge-filescompressed-files

Read lines from compressed text files


Is it possible to read a line from a gzip-compressed text file using Python without extracting the file completely? I have a text.gz file which is around 200 MB. When I extract it, it becomes 7.4 GB. And this is not the only file I have to read. For the total process, I have to read 10 files. Although this will be a sequential job, I think it will a smart thing to do it without extracting the whole information. How can this be done using Python? I need to read the text file line-by-line.


Solution

  • Have you tried using gzip.GzipFile? Arguments are similar to open.