Reading text files with Python is a common task and can be done in several ways. In this article, we will cover the most popular methods for reading text files in Python. For the examples, I’ll use a file called names.txt with one name per line:
Alice
Bob
CharlieCode language: plaintext (plaintext)
Read a Text File Using with open()
The modern and recommended way to read a text file in Python is to use the with open statement:
- The
withstatement automatically closes the file after the indented block of code is executed, ensuring the file is always closed properly. - The
open()function takes the file path as its first argument and returns a file object that can be used to read the file’s contents, for example all at once with theread()method. It accepts more arguments, like the mode ('r'for reading is the default) and theencodingof the file.
Here is an example of how to open and read a text file in Python this way:
with open('names.txt', encoding='utf-8') as f:
# Read the contents of the file into a variable
names = f.read()
# Print the names
print(names)Code language: Python (python)
Output:
Alice Bob Charlie
The empty line at the end is there because the file ends with a newline, and print() adds another one.
I recommend always passing encoding='utf-8'. Since Python 3.15, UTF-8 is the default encoding, but older Python versions use the system’s default, which on Windows often isn’t UTF-8. Being explicit makes your code read files the same way everywhere. If the file doesn’t exist, open() raises a FileNotFoundError.
Read a file line by line
Often, you want to process a file one line at a time. A file object is iterable, so you can loop over it with a for-loop:
with open('names.txt', encoding='utf-8') as f:
for line in f:
print(line.strip())Code language: Python (python)
Output:
Alice Bob Charlie
Each line still ends with a newline character, which is why I call strip(). It removes whitespace, including that newline, from both ends of the line. This method reads only one line at a time, so it also works for files that are too big to fit in memory.
Read all lines into a list
If you’d rather have all lines in a list, you can use readlines(). It keeps the newline at the end of each line, though:
with open('names.txt', encoding='utf-8') as f:
lines = f.readlines()
print(lines)Code language: Python (python)
Output:
['Alice\n', 'Bob\n', 'Charlie\n']
Usually, you don’t want those newlines. Reading the whole file and splitting it with splitlines() gives you a clean list:
with open('names.txt', encoding='utf-8') as f:
names = f.read().splitlines()
print(names)Code language: Python (python)
Output:
['Alice', 'Bob', 'Charlie']
Read a Text File Using pathlib
The shortest way to read a whole text file is with the Path class from the built-in pathlib module. Its read_text() method opens the file, reads it, and closes it again, all in one call:
from pathlib import Path
names = Path('names.txt').read_text(encoding='utf-8')
print(names.splitlines())Code language: Python (python)
Output:
['Alice', 'Bob', 'Charlie']
If you already use Path objects to work with file paths, this is the most natural choice. For large files that you want to process line by line, stick with the for-loop from above.
Read our more extensive article about using files in Python to learn more.
Using open() and close() manually
The more basic method for reading a text file in Python is to use the open() and close() functions manually. I recommend you use the first method for most cases since you don’t have to worry about closing the file, which can be easily forgotten. Here is an example anyway:
f = open('names.txt', encoding='utf-8')
# Read the contents of the file into a variable
names = f.read()
print(names)
# Don't forget to close the file again
f.close()Code language: Python (python)
If an error happens between open() and close(), the file is never closed. That’s another reason to prefer with.
Read a Text File Using Pandas
If your text file contains structured data, like a table, and you want to analyze or manipulate that data, you may want to use the pandas library. This library provides a powerful and easy-to-use DataFrame object that can read and manipulate text files. You need to install it first, for example with pip install pandas.
To read text files into a DataFrame, the file needs to have a clearly defined format. Since this usually is CSV, let’s focus on that. Here is an example of reading a CSV file into a DataFrame:
import pandas as pd
# Read a CSV file into a DataFrame
df = pd.read_csv('example.csv')
print(df)Code language: Python (python)
In addition to reading CSV files, pandas also provides functions for reading other types of text files, such as read_json() for reading JSON files, and read_html() for reading HTML tables.
Here’s an example of reading a JSON file into a DataFrame:
import pandas as pd
# Read a JSON file into a DataFrame
df = pd.read_json('example.json')
print(df)Code language: Python (python)
Read a Text File Using NumPy
NumPy is a library for working with arrays and matrices of numerical data. It provides several functions for reading text files, including loadtxt() and genfromtxt().
numpy.loadtxt() allows parameters that specify how the data is delimited, which rows and columns to skip, and how to handle missing data. For example, to specify that the data is delimited by commas (typical for a CSV file), you can pass the delimiter parameter. If example.csv contains the lines 1,2,3 and 4,5,6, this code:
import numpy as np
# Read a CSV file into a NumPy array
data = np.loadtxt('example.csv', delimiter=',')
print(data)Code language: Python (python)
Gives this output:
[[1. 2. 3.] [4. 5. 6.]]
By default, loadtxt() turns every value into a floating-point number, which is why you see 1. instead of 1. Pass dtype=int if you want integers.