How can I read large text files line by line, without loading them into memory? [duplicate]

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)

Leave a Comment