On this page

How to Read a File in Python

Enrico Hochkoeppler Profile

By Enrico Hochkoeppler

Web Developer & TutoriaLibre Founder

Python provides built-in tools for reading files that require no external libraries for the most common cases. The standard pattern is: open a file with open(), read its contents using one of several methods, and let Python handle cleanup automatically via a with statement. This lesson covers every reading method, when to use each, and how to avoid the most common mistakes.


File Modes: What "r", "rb", and Others Mean​

Every call to open() requires a mode argument that tells Python what you intend to do with the file. For reading, the two relevant modes are:

ModeMeaning"r"Read mode — plain text (default)"rb"Read binary mode — raw bytes, no text decoding

There are other modes ("w", "a", "r+") used for writing or combined operations, but when you only need to read, you will use "r" for text files and "rb" for binary files such as images, PDFs, or executables.

Always specify the encoding parameter when opening text files. If you omit it, Python uses the operating system's default encoding, which varies between platforms and is a common source of bugs on Windows:


with open("data.txt", "r", encoding="utf-8") as f:

    content = f.read()

"utf-8" is the correct choice for the vast majority of modern text files.


The open() Function​

open() is a Python built-in that returns a file object. Its most important parameters are:


open(file, mode="r", encoding=None, errors=None, newline=None)

A bare call without a with statement looks like this:


f = open("data.txt", "r", encoding="utf-8")

content = f.read()

f.close()  # Required — and easy to forget

The problem with this pattern is that if an exception occurs between open() and close(), the file stays open, consuming a system resource until the interpreter exits. This is why the with statement exists.


Using with open() — The Correct Pattern​

The with statement is a context manager. When the indented block ends — whether normally or due to an exception — Python automatically calls f.close() on your behalf:


with open("data.txt", "r", encoding="utf-8") as f:

    content = f.read()

# The file is guaranteed to be closed here

This is not just a stylistic preference — it is the correct way to handle files in Python. All examples in this lesson use this pattern.


Reading the Entire File at Once: .read()​

.read() loads the complete file content into a single string:


with open("notes.txt", "r", encoding="utf-8") as f:

    text = f.read()



print(text)

If you pass an integer argument, .read(n) reads at most n characters, leaving the file cursor at that position:


with open("notes.txt", "r", encoding="utf-8") as f:

    first_ten = f.read(10)   # Reads the first 10 characters

    next_five = f.read(5)    # Reads the next 5 characters

When to use .read(): configuration files, small templates, files where you need the entire content as a string for text processing (searching, replacing, parsing).

When to avoid it: large files. Loading a 2 GB log file into a string will consume 2 GB of memory. For large files, use the line-by-line techniques described below.


Reading All Lines into a List: .readlines()​

.readlines() reads the entire file and returns a list of strings, one per line. Each string includes the trailing \n newline character:


with open("log.txt", "r", encoding="utf-8") as f:

    lines = f.readlines()



# lines is now ['First line\n', 'Second line\n', 'Third line\n']

To remove the trailing newline from each element, use .strip():


lines = [line.strip() for line in lines]

When to use .readlines(): when you need random access to lines by index, or when you want to load all lines first and then process them in a non-sequential order (e.g., reversing, sorting, slicing).

When to avoid it: large files, for the same memory reason as .read().


Reading One Line at a Time: .readline()​

.readline() reads a single line from the file each time it is called. It returns an empty string "" when it reaches the end of the file:


with open("log.txt", "r", encoding="utf-8") as f:

    line = f.readline()

    while line:

        print(line.strip())

        line = f.readline()

When to use .readline(): when you need precise, manual control over which lines to read — for example, to skip a header line and then read the rest, or to process a fixed number of lines.


Iterating Directly Over the File — The Most Efficient Pattern​

A file object is itself an iterable. Looping over it directly yields one line at a time without loading the whole file into memory. This is the most memory-efficient approach and the one considered most idiomatic in Python:


with open("large_log.txt", "r", encoding="utf-8") as f:

    for line in f:

        print(line.strip())

Python reads the file in buffered chunks internally. Your code processes one line at a time while only a small buffer is held in memory — regardless of the total file size. This pattern scales to files of any size.

This is the recommended approach for large files, log processing, and data pipelines.


Reading Binary Files: "rb" Mode​

Binary mode reads the raw bytes of a file without any text decoding. This is necessary for images, audio files, executables, PDFs, and any format that is not plain text:


with open("photo.jpg", "rb") as f:

    data = f.read()



print(type(data))   # <class 'bytes'>

print(data[:4])     # First 4 bytes, e.g. b'\xff\xd8\xff\xe0' for JPEG

In binary mode, .read() returns a bytes object, not a string. You cannot use encoding in binary mode — it raises a ValueError.


Reading Specific Parts of a File​

To jump to a specific position in a file, use .seek(offset) to move the cursor and .tell() to find out where the cursor currently is:


with open("data.txt", "r", encoding="utf-8") as f:

    f.seek(20)           # Move to byte position 20

    portion = f.read(5)  # Read 5 characters from that position

    print(f.tell())      # Prints current position (25)

This is particularly useful for reading structured binary files or when you already know the byte offset of the data you need.


Using pathlib for File Paths​

The pathlib module, available in Python 3.4+, provides an object-oriented interface for file paths that works consistently across Windows, macOS, and Linux:


from pathlib import Path



path = Path("data") / "notes.txt"



# Read the entire file in one line

text = path.read_text(encoding="utf-8")

Path.read_text() handles opening and closing the file automatically. It is equivalent to with open(...) as f: return f.read().

For binary files, use Path.read_bytes():


data = Path("photo.jpg").read_bytes()

pathlib also lets you check whether a file exists before attempting to read it:


path = Path("config.txt")

if path.exists():

    text = path.read_text(encoding="utf-8")

Handling Errors: FileNotFoundError and UnicodeDecodeError​

Two errors arise most commonly when reading files.

FileNotFoundError occurs when the specified path does not exist:


try:

    with open("missing.txt", "r", encoding="utf-8") as f:

        data = f.read()

except FileNotFoundError:

    print("The file does not exist.")

UnicodeDecodeError occurs when the file contains bytes that are not valid in the specified encoding. This happens when a file was saved in a different encoding (e.g., Latin-1 or Windows-1252) but you attempt to open it as UTF-8:


try:

    with open("legacy.txt", "r", encoding="utf-8") as f:

        data = f.read()

except UnicodeDecodeError:

    # Try again with the actual encoding

    with open("legacy.txt", "r", encoding="latin-1") as f:

        data = f.read()

If you do not know the encoding of a file and cannot change it, pass errors="replace" to substitute undecodable bytes with a placeholder character, or errors="ignore" to skip them silently. Neither is ideal for data integrity, but both prevent the program from crashing.


Reading CSV and JSON Files​

The same open() and with pattern underpins Python's specialized file-reading modules.

CSV files are read with the csv module, which parses comma-separated rows into Python lists:


import csv



with open("data.csv", "r", encoding="utf-8", newline="") as f:

    reader = csv.reader(f)

    for row in reader:

        print(row)  # Each row is a list of strings

Note the newline="" argument — this is the correct way to open CSV files in Python, as it prevents the csv module from mishandling line endings on Windows.

JSON files are read with the json module, which converts JSON text directly into Python dictionaries and lists:


import json



with open("config.json", "r", encoding="utf-8") as f:

    config = json.load(f)



print(config["server"])  # Access like a regular dictionary

Comparison of Reading Methods​

MethodReturnsLoads entire file?Best for.read()``strYesSmall files, full-text processing.read(n)``strNoReading a fixed number of characters.readlines()``list[str]YesRandom access to lines by index.readline()``strNoManual, line-by-line controlfor line in f``str (one at a time)NoLarge files, streaming, data pipelinesPath.read_text()``strYesShort, clean one-liners for small files


Common Mistakes to Avoid​

Not specifying encoding. Omitting encoding="utf-8" means the encoding is determined by the operating system. Code that works on Linux may fail on Windows for the same file.

Using .read() on large files. Loading a large file entirely into memory causes unnecessary resource consumption. Use for line in f instead.

Forgetting newline="" when opening CSV files. Without it, the csv module may see extra blank rows on Windows due to how it handles \r\n line endings.

Calling .close() manually without a try/finally block. If an exception occurs before .close() is reached, the file stays open. Use with open(...) instead — always.

Comparing file content immediately after writing without seeking back to position 0. If you open a file in "r+" mode, write to it, and then call .read() without calling .seek(0) first, you will read from the current cursor position, not from the beginning.


Best Practices Summary​

Found this helpful? Share the knowledge...

... or support this project to keep it alive!

Buy Me a Coffee

Previous

File Handling & Persistence

Next

Writing Files