Chapters
Python114 chapters

ReferenceFile methods

file.readline()

Reads one line, including its newline.

Signature

f.readline()

Returns

A string, empty at the end of the file.

Example

Python
with open("names.txt", "w", encoding="utf-8") as f:
    f.write("Ada\nGrace\n")

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

Output

'Ada\n'
'Grace\n'
''

See also