Python file.readlines() Method

Beginner
6 min read
Updated: Nov 2026
3 examples
file method

Definition

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 than hint 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)
CallWhat 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

ParameterDescription
hintOptional. Stop after the line that makes the total size exceed hint. Lines are never split. 0, negative, or omitted reads everything.

Return Value

Return ValueDescription
listLines 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])

How It Works

  1. One list. readlines() returns all three tasks; the file is closed right after by the with block, but the list stays usable.
  2. Newlines kept. Printing the list shows \n at the end of every item.
  3. 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])

How It Works

  1. Five short lines. Each line such as id,1\n is 5 characters long.
  2. 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.
  3. Leftovers. Only two lines remain for the second batch, and the next call returns [], ending the loop.
  4. 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())

How It Works

  1. Exact bytes on disk. Writing in binary mode stores \r\n on every OS, like a file made on Windows.
  2. Binary keeps everything. "rb" returns the raw byte lines with \r\n intact.
  3. Default text mode translates. With newline=None, universal newlines turn each \r\n into \n.
  4. newline="" keeps but splits. Lines still split at \r\n, yet the characters are returned untranslated, which is what the csv module expects.

Frequently Asked Questions

No. Every item keeps its "\n". Use [line.rstrip("\n") for line in f] or f.read().splitlines() for lines without newlines.
It stops reading after the line that makes the total length exceed hint. Lines are never split, so a batch can be a little larger than hint.
It loads every line into memory at once. For large files, loop with for line in f or read in batches with a hint.
readline() returns one line as a string; readlines() returns all remaining lines as a list of strings.
An empty list. That makes "if not batch: break" a natural loop exit when you read in batches.

Next: file.reconfigure()

Change the encoding, errors, newline, or buffering settings of an open text file (Python 3.7+).

reconfigure() →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

12 people found this page helpful