file.readline() Read one line
Last updated: 2026-09-22
file.readline() Read one line
file.readline(size=-1) reads one line; returns an empty string at end of file.
| Category | File Methods |
|---|---|
| Kind | Built-in Type Method |
| Python Version | all |
📝 Syntax
file.readline()
⚙️ Parameters
| size Optional | Maximum number of chars to read; -1 reads a whole line.(Default:-1) |
|---|
Returns:One line (including the trailing newline); an empty string at EOF.
Mutates the original:No (returns a new object)
💥 Raises
OSError— Raised when the file is closed or on I/O errors.
▶ Example
Edit the code, press Run, and see the result in the output panel:
Output:
'line1\n'
'line2\n'
''
💡 Related Cases
More case studies coming soon — check back later.
❓ FAQ
QWhat does readline() read?
AThe content of one line (including the trailing newline). At the end of the file it returns the empty string '', which is the standard way to detect EOF.
QWhat if the last line has no newline?
AThat line is still returned as-is; only when there is truly no content to read is '' returned.
QWhat is the size parameter for?
AIt limits the maximum number of characters read on this line; it is rarely needed.
QWhat is the recommended way to read a file line by line?
Afor line in f: iterating directly over the file object is the simplest and most efficient; readline() suits scenarios where you need manual control over the reading pace.