rochacbruno rochacbruno - 7 months ago 55
Python Question

Read large text files in Python, line by line without loading it in to memory

I need to read a large file, line by line. Lets say that file has more than 5GB and I need to read each line, but obviously I do not want to use

because it will create a very large list in the memory.

How will the code below work for this case? Is
itself reading one by one into memory? Is the generator expression needed?

f = (line for line in open("log.txt").xreadlines()) # how much is loaded in memory?

Plus, what can I do to read this in reverse order, just as the Linux

I found:


"python head, tail and backward read by lines of a text file"

Both worked very well!


I provided this answer because Keith's, while succinct, doesn't close the file explicitly

with open("log.txt") as infile:
    for line in infile: