Mastering Python’s readline() – Efficient Line‑by‑Line File Reading
What is Python readline()?
readline() is a built‑in file method that returns the next full line from a file object. The returned string ends with a newline character (\n). When the end of the file is reached, it returns an empty string.
You can optionally pass a size argument to limit the number of bytes read. A negative or omitted value causes the entire line to be returned.
Key Characteristics
- Reads one complete line per call.
- Appends a trailing newline unless the line is the last one without one.
- Returns a string in text mode; a bytes object in binary mode.
- Supports an optional
sizeparameter (default is -1).
Syntax
file_object.readline([size])
Parameters
- size (int, optional): Number of bytes to read. Default is -1 (read whole line).
Return Value
A string (or bytes) containing the next line, or an empty string when the file is exhausted.
Example 1: Read the First Line
Assume demo.txt contains:
Testing - FirstLine
Testing - SecondLine
Testing - Third Line
Testing - Fourth Line
Testing - Fifth Line
Python code:
with open("demo.txt", "r") as f:
first_line = f.readline()
print(first_line)
Output:
Testing - FirstLine
Example 2: Use the size Argument
Read only the first 10 characters of the first line:
with open("demo.txt", "r") as f:
partial = f.readline(10)
print(partial)
Output:
Testing -
Basic File I/O in Python
Opening files is handled by open(path, mode). Common modes:
| Mode | Description |
|---|---|
| r | Read (default) |
| w | Write (truncate) |
| a | Append |
| rb | Read binary |
| wb | Write binary |
Reading a File Line‑by‑Line
Using a while loop with readline():
with open("demo.txt", "r") as f:
line = f.readline()
while line:
print(line)
line = f.readline()
Using a for loop (preferred for readability):
with open("demo.txt", "r") as f:
for line in f:
print(line)
Read All Lines at Once
Python’s readlines() returns a list of all lines:
with open("test.txt", "r") as f:
lines = f.readlines()
print(lines)
Example output:
["Line No 1\n", "Line No 2\n", "Line No 3\n", "Line No 4\n", "Line No 5"]
Summary
- Use
readline()for controlled, line‑by‑line reading. - Pass
sizeto limit bytes read; default reads the entire line. - For simple iteration, prefer
for line in fileover a manualwhileloop. - When you need all lines in memory,
readlines()is convenient. - Always close files; the
withstatement handles this automatically.
Python
- Python Print() Function: A Practical Guide with Examples
- Master Python’s str.count(): How to Count Characters & Substrings with Examples
- Master Python `format()` Strings with Clear Examples
- Master Python's String.find() Method: Syntax, Examples & Alternatives
- Master Python Lambda Functions: Practical Examples & Best Practices
- Python round() Function Explained with Practical Examples
- Mastering Python's map() Function: Syntax, Examples, and Best Practices
- Python timeit() – Measuring Execution Time with Practical Examples
- Python list.count(): Expert Guide with Practical Examples
- Python Module Importing – A Practical Guide with Examples