Python Principles
Python Q&A

How do I read a large file line by line in Python?

Answer:

Use open to open the file, use the with keyword to ensure the file is closed again when you're done with it, and use a for loop to iterate over every line. For example:

with open("file.txt") as f:
    for line in f:
        print(line)

Note that the lines will include their terminating newline. In other words, each line will end in \n, a newline. To remove it, you can use:

line = line.rstrip()