Python Language Files & Folders I/O Reading a file line-by-line


The simplest way to iterate over a file line-by-line:

with open('myfile.txt', 'r') as fp:
    for line in fp:

readline() allows for more granular control over line-by-line iteration. The example below is equivalent to the one above:

with open('myfile.txt', 'r') as fp:
    while True:
        cur_line = fp.readline()
        # If the result is an empty string
        if cur_line == '':
            # We have reached the end of the file

Using the for loop iterator and readline() together is considered bad practice.

More commonly, the readlines() method is used to store an iterable collection of the file's lines:

with open("myfile.txt", "r") as fp:
    lines = fp.readlines()
for i in range(len(lines)):
    print("Line " + str(i) + ": " + line)

This would print the following:

Line 0: hello

Line 1: world