How can I read large text files line by line, without loading it into memory?

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:
        do_something_with(line)

Leave a Comment