How to Read a File in Python

The fastest way to read a file in Python is the with statement and the read() method:

with open("datos.txt", "r") as archivo:
    contenido = archivo.read()

When do you need this?

Reading files is one of the most common tasks in Python. You need it every time your program loads settings, processes reports, reads logs or opens any text file. In this guide you will learn how to read a file in Python step by step, from the simplest example to the safest one.

Method 1: Open a file with open() and read()

The most direct way to read a file in Python is to open it and call the read() method.

archivo = open("datos.txt", "r")
contenido = archivo.read()
archivo.close()
print(contenido)

The first argument is the file name and the second one, "r", means read mode. Always close the file with close() to release the resources.

The best practice in Python is to use the with statement. It closes the file automatically, even if an error occurs.

with open("datos.txt", "r") as archivo:
    contenido = archivo.read()
print(contenido)

This is the recommended way to read a file in Python because it is safer and easier to read.

Method 3: Read line by line or into a list

If the file is very large, reading everything at once can use a lot of memory. You can iterate over the file to process it line by line.

with open("datos.txt", "r") as archivo:
    for linea in archivo:
        print(linea, end="")

The readlines() method returns a list where each element is one line of the file.

with open("datos.txt", "r") as archivo:
    lineas = archivo.readlines()
print(lineas)

Method 4: Specify UTF-8 encoding

To read files with accents or special characters, indicate the utf-8 encoding. This is essential when working with text in Spanish.

with open("datos.txt", "r", encoding="utf-8") as archivo:
    contenido = archivo.read()

Method comparison

Method Advantage When to use it
open() + read() Simple and direct Quick scripts, small files
with open() Safe, closes automatically Always recommended
for loop Low memory usage Very large files
readlines() Returns a list of lines When you need every line
encoding="utf-8" Reads accents and symbols Files in Spanish or other languages

Common errors and solutions

FileNotFoundError: the file does not exist

If the file does not exist, Python raises a FileNotFoundError. Use try and except to handle it gracefully.

try:
    with open("datos.txt", "r", encoding="utf-8") as archivo:
        contenido = archivo.read()
except FileNotFoundError:
    print("El archivo no existe")

UnicodeDecodeError: the file has another encoding

If the file was saved in a different format, add the correct encoding argument to the open() call.

Frequently asked questions

What does the "r" in open() mean?

The "r" stands for read mode. It opens the file to read its content without modifying it.

Do I need to close the file with the with statement?

No. The with statement closes the file automatically. That is why it is the recommended way to read a file in Python.

How do I read a file line by line?

Use a for loop over the file object, as shown in Method 3.

how to read a file in python, python read file, open file python, read text file python, python with open, readlines python, utf-8 python, file not found python

Save this tutorial and share it if it helped you. Find more practical guides in the Python how-to section.