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.

CategoryFile Methods
KindBuilt-in Type Method
Python Versionall

📝 Syntax

file.readline()

⚙️ Parameters

size OptionalMaximum 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

▶ Example

Edit the code, press Run, and see the result in the output panel:

Output:

'line1\n'
'line2\n'
''
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.
Web-Tutorial.com

Web-Tutorial Tech Team

A team of developers maintaining programming tutorials. Each tutorial is written and reviewed by developers with expertise in that field. We work to keep our content accurate and reliable — if you spot an issue, please let us know.

100%

🙏 帮我们做得更好

我们是刚上线的编程教程站,几个人的小团队,精力有限。页面虽经检查,难免还有疏漏——链接失效、排版错乱、内容有误、语言生硬……

如果您发现了,麻烦告诉我们,我们会在收到反馈后第一时间进行修复,再次感谢您的光临 🙏