A for loop reads a file from top to bottom, one line per repetition, and it
never stops half way. Sometimes you want more control than that: read only the
first line, or read the lines yourself and decide when to stop. That is what
.readline() is for.
.readline() reads one line from the file and gives it back as a string,
newline included. Each time you use it, it moves on to the next line.
my_file = open("singers.txt", "r")
first = my_file.readline()
second = my_file.readline()
third = my_file.readline()
my_file.close()
print(first.strip())
print(second.strip())
print(third.strip())
With the singers.txt file from the previous page, this prints:
Kurt
Debbie
Freddie
The other eight names are never read. The program closed the file after three lines, and nothing forces it to read the rest.
That is exactly what you need when a file holds one value, such as a saved high score or a code: read the first line, close the file straight away, and use what you read.
A text file only holds text. A file containing the number 250 really contains
the characters 2, 5 and 0, followed by a newline, so .readline() gives
you the string "250\n". To do sums with it, turn it into a number with
int(), the same as with anything you read with input().
score_file = open("high_score.txt", "r")
high_score = score_file.readline()
score_file.close()
high_score = int(high_score)
print("To beat the high score you need " + str(high_score + 1))
If high_score.txt holds 250, this prints
To beat the high score you need 251.
int() does not mind the newline: it ignores spaces and newlines at the start
and the end of the string, so int("250\n") is 250. It does mind anything
else, so a line that holds a word, or a line that is completely empty, stops the
program with a ValueError.
When there are no lines left, .readline() gives back an empty string,
"": a string with nothing in it at all, not even a newline.
That is how a program can tell it has reached the end. A line in the middle of
a file is never "". Even a blank line in the middle still has its newline, so
it is "\n".
Put .readline() and the empty string together and you can read a whole file
with a condition controlled loop: keep reading while the line you just read
is not empty.
my_file = open("singers.txt", "r")
line = my_file.readline()
while line != "":
print(line.strip())
line = my_file.readline()
my_file.close()
This prints all eleven names, the same as the for loop did. Look at where the
two .readline() lines are:
That is the same shape as a counter in a while loop: set it up before the loop,
check it in the condition, and change it at the end of the body. Leave out the
second .readline() and line never changes, so the loop never ends.
If the file is empty, the very first .readline() gives back "", the
condition is false straight away, and the body never runs at all.
A for loop over the file does all of this for you, just as a for loop over a range does the counting for you. The while loop is worth knowing because it shows how the reading actually works, and because you can add more to its condition when a program has to stop early.
Make sure that you check for the following things:
.readline() has its brackets, and is used on the file variable:
my_file.readline()..readline() reads a new line. Use it once and store the result if
you need the same line twice.int() before you do sums with it
or compare it with a number..readline() goes before the loop and one goes at the
end of the loop’s body."".