readlines() reads all remaining lines and returns them as a list. Every item keeps its newline, exactly as readline() would return it.
The optional hint stops reading once the lines collected so far add up to more thanhint characters or bytes, which lets you process a big file in batches.
file.readlines() is a method of the file objects that the built-in open() returns, so no import is needed. It works on text files (TextIOWrapper), binary files (BufferedReader, BufferedWriter, BufferedRandom), and raw files (FileIO, opened with buffering=0).
Syntax
Call it with no argument for every remaining line, or pass a size hint.
python
lines = f.readlines()
batch = f.readlines(hint)
Call
What it does
f.readlines()
A list of every remaining line, newlines included.
f.readlines(10)
Lines until their total length passes 10; whole lines only.
f.readlines()
At the end of the file: an empty list [].
list(f) gives the same list. For a plain loop, for line in f: avoids building the list at all.
Method signatures
python
IOBase.readlines(hint=-1, /) # -> list[str] in text mode, list[bytes] in binary mode
# hint <= 0 or omitted: read every remaining line
Parameters
Parameter
Description
hint
Optional. Stop after the line that makes the total size exceed hint. Lines are never split. 0, negative, or omitted reads everything.
Return Value
Return Value
Description
list
Lines as str (text mode) or bytes (binary mode), each with its newline.
[]
When the position is already at the end of the file.
Python Examples
Each example works inside a temporary folder that is deleted at the end, so nothing is left on your disk.
Example 1 — Load a To-Do File as a List
Read every task at once, then strip the newlines for display.
python
import os
import tempfile
with tempfile.TemporaryDirectory() as folder:
path = os.path.join(folder, "todo.txt")
with open(path, "w", encoding="utf-8") as f:
f.write("buy milk\ncall Sam\nwater plants\n")
with open(path, encoding="utf-8") as f:
lines = f.readlines()
print(lines)
tasks = [line.rstrip("\n") for line in lines]
print(len(tasks), tasks[-1])
Output
['buy milk\n', 'call Sam\n', 'water plants\n']
3 water plants
How It Works
One list.readlines() returns all three tasks; the file is closed right after by the with block, but the list stays usable.
Newlines kept. Printing the list shows \n at the end of every item.
Clean copy. The comprehension strips each newline, so the last task prints as plain text.
Example 2 — Read IDs in Batches with a hint
Process five 5-character lines in batches using readlines(10).
python
import os
import tempfile
with tempfile.TemporaryDirectory() as folder:
path = os.path.join(folder, "ids.csv")
with open(path, "w", encoding="utf-8") as f:
for n in range(1, 6):
f.write(f"id,{n}\n")
with open(path, encoding="utf-8") as f:
while True:
batch = f.readlines(10)
if not batch:
break
print(len(batch), [line.strip() for line in batch])
Output
3 ['id,1', 'id,2', 'id,3']
2 ['id,4', 'id,5']
How It Works
Five short lines. Each line such as id,1\n is 5 characters long.
Hint is a threshold. After two lines the total is exactly 10, which does not exceed the hint, so a third line is added: the first batch has 15 characters.
Leftovers. Only two lines remain for the second batch, and the next call returns [], ending the loop.
Whole lines only. The hint never cuts a line in half; it only decides when to stop adding lines.
Example 3 — Compare \r\n Handling in Three Modes
Read the same Windows-style file in binary mode, default text mode, and text mode with newline="".
python
import os
import tempfile
with tempfile.TemporaryDirectory() as folder:
path = os.path.join(folder, "settings.ini")
with open(path, "wb") as f:
f.write(b"x=1\r\ny=2\r\n")
with open(path, "rb") as f:
print(f.readlines())
with open(path, encoding="utf-8") as f:
print(f.readlines())
with open(path, encoding="utf-8", newline="") as f:
print(f.readlines())