["python","file-io"]
How can I read large text files line by line, without loading them into memory?

I want to read a large file (>5GB), line by line, without loading its entire contents into memory. I cannot use readlines() since it creates a very large list in memory.
Use a for loop on a file object to read it line-by-line. Use with open(...) to let a context manager ensure that the file is closed after reading: with open("log.txt") as infile: for line in infile: print(line)