ZetCode

Python readline function

last modified November 29, 2026 Python 3.15

In this article we show how to use the readline method to read a file one line at a time. We look at the newline that each line keeps, the size argument, the empty string at the end of the file, loops, and the way readline interacts with iteration.

Opening files is explained in Python open function, reading a fixed number of characters in Python read function, and reading all lines into a list in Python readlines function.

The text file

The examples work with a small file of city names, saved in UTF-8 with one name per line. Every line, including the last one, ends with a newline.

cities.txt
Paris
Prague
Vienna
Kraków
Budapest
Warsaw

The accented ó in Kraków takes two bytes in UTF-8 but counts as a single character, which matters for every method that counts one or the other.

Reading one line

readline returns the next line as a string, together with its terminator. The position moves to the start of the following line, so repeated calls walk through the file.

one_line.py
with open('cities.txt', encoding='utf-8') as f:
    print(repr(f.readline()))
    print(repr(f.readline()))
    print(repr(f.readline()))

The repr output shows the \n at the end of each string. Printing the string directly would add a second newline, because print appends one of its own to the one already in the line.

$ python one_line.py
'Paris\n'
'Prague\n'
'Vienna\n'

The size cap

With an argument, readline returns at most that many characters, or the rest of the line, whichever comes first. When the cap is reached before the newline, the returned string does not end with \n and the next call continues in the same line.

line_cap.py
with open('notes.txt', 'w', encoding='utf-8') as f:
    f.write('shopping list for the weekend\n')
    f.write('milk\n')

with open('notes.txt', encoding='utf-8') as f:
    print(repr(f.readline(8)))
    print(repr(f.readline(8)))
    print(repr(f.readline()))

The long first line is delivered in pieces: eight characters, eight more, then the remaining 14 including the newline. The second line is not touched until the first one is finished. A size larger than the line simply returns the whole line.

$ python line_cap.py
'shopping'
' list fo'
'r the weekend\n'

The end of the file

At the end of the file readline returns an empty string. The test is the same as for read: a real line is never empty, because it always contains at least its newline.

line_eof.py
with open('cities.txt', encoding='utf-8') as f:
    print(len(f.read()))
    print(repr(f.readline()))
    print(repr(f.readline()))

After the first call the position is at the end, and both readline calls return ''. The returned string can therefore serve as the condition of a loop.

$ python line_eof.py
43
''
''

Reading a file line by line

The walrus operator keeps the loop compact: the line is assigned and tested in the condition, and the loop ends on the empty string.

line_loop.py
with open('cities.txt', encoding='utf-8') as f:
    while line := f.readline():
        print(line.rstrip('\n').upper())

Six lines are printed in capitals. rstrip('\n') removes only the terminator, so spaces that belong to the data would be kept; strip() would remove them as well.

$ python line_loop.py
PARIS
PRAGUE
VIENNA
KRAKÓW
BUDAPEST
WARSAW

Mixing readline with iteration

A file object is its own iterator, and iteration continues from the current position. A line read with readline is not repeated by a following for loop.

line_iteration.py
with open('cities.txt', encoding='utf-8') as f:
    print(repr(f.readline()))
    for line in f:
        print(repr(line))

The first call consumes Paris\n, and the loop yields the remaining five lines with their newlines. This makes readline useful for a header, but it also means that the two styles must not be mixed without keeping the position in mind.

$ python line_iteration.py
'Paris\n'
'Prague\n'
'Vienna\n'
'Kraków\n'
'Budapest\n'
'Warsaw\n'

A header and the rest

Many files start with a header line followed by records. readline takes the header, and a loop reads the records.

line_header.py
with open('notes.txt', 'w', encoding='utf-8') as f:
    f.write('shopping list\nmilk\nbread\neggs\n')

with open('notes.txt', encoding='utf-8') as f:
    header = f.readline().rstrip('\n')
    items = []
    while line := f.readline():
        items.append(line.rstrip('\n'))

print(header)
print(len(items), items)

The header is cleaned and stored separately; the loop appends each remaining line without its newline. The list has three items, because the first call already consumed the header. Python readlines function shows the alternative that returns the whole file as a list.

$ python line_header.py
shopping list
3 ['milk', 'bread', 'eggs']

Source

IOBase.readline – Python documentation

Methods of File Objects – Python Tutorial

In this article we have read single lines, capped their length, stopped on the empty string, and combined readline with iteration and with a header. See also Python read function, Python readlines function, Python read file, and Python open function.

Author

I am Jan Bodnar, a programmer and technical writer. I have been writing programming tutorials at ZetCode since 2007 and have published eight e-books. See about the author.

List all Python tutorials.