HOWTO · Python
How to Read a Text File Into a List in Python
This tutorial demonstrates how to read a text file into a Python list. Explore various methods, including readlines(), list comprehensions, and more, to efficiently manipulate text files in Python. Enhance your programming skills with clear examples and detailed explanations.
On this page
To read a text file into a Python list, first decide what each list item should represent: a physical line, a delimited field, a row, or a numeric value. For ordinary text, readlines() reads the file and accumulates one string per line in its result list, keeping line endings when present. By contrast, read() followed by splitlines() holds the complete file text and then creates a list of line strings without their line endings. If you only need to process a large file, iterate over it instead of collecting every line.
The examples below create their own small input files, so each can be adapted by replacing the temporary path with your file path. They are illustrative examples and were not executed in this writing stage.
Read lines with readlines()
readlines() returns a list whose elements are the file’s lines. A line ending is usually included in each string when present, so it can appear as \n when the list is displayed. The context manager closes the file after the list has been read.
Run this example:
from pathlib import Path
from tempfile import TemporaryDirectory
with TemporaryDirectory() as directory:
path = Path(directory) / "notes.txt"
path.write_text("First line\nSecond line\nThird line\n", encoding="utf-8")
with path.open(encoding="utf-8") as file:
lines = file.readlines()
print(lines)
The resulting list contains all lines in memory, including their line endings. This is convenient for small files when you need to keep each original line string. For a large file that only needs processing, use the loop in the large-file section rather than storing every line.
Read and clean lines with a list comprehension
Iterating over a text file reads it one line at a time. A list comprehension can turn those lines into a list, while rstrip("\r\n") removes line-ending characters without removing spaces that are part of the text.
from pathlib import Path
from tempfile import TemporaryDirectory
with TemporaryDirectory() as directory:
path = Path(directory) / "names.txt"
path.write_text(" Ada \nLin\n", encoding="utf-8")
with path.open(encoding="utf-8") as file:
names = [line.rstrip("\r\n") for line in file]
print(names)
strip() is another option, but it removes whitespace from both ends, including meaningful spaces in a line. Although the comprehension reads its input incrementally, the final list still holds every cleaned string in memory. Choose it when a list is needed and the file fits the available memory.
Read the whole file and split it into lines
Use read() followed by splitlines() when the complete text is needed before it is divided into lines. splitlines() recognizes common line boundaries and does not keep those separators in the returned strings.
from pathlib import Path
from tempfile import TemporaryDirectory
with TemporaryDirectory() as directory:
path = Path(directory) / "poem.txt"
path.write_text("Red\nBlue\nGold\n", encoding="utf-8")
with path.open(encoding="utf-8") as file:
content = file.read()
lines = content.splitlines()
print(lines)
Both the complete string and the resulting list take memory, so this is a poor fit for very large inputs. For line-oriented work, a comprehension or loop avoids first building a second copy of the entire file as one string.
Process large files line by line
When each record can be handled independently, process it inside the file loop and keep only the state the task needs. This example counts nonblank lines without retaining the file’s contents as a list.
from pathlib import Path
from tempfile import TemporaryDirectory
with TemporaryDirectory() as directory:
path = Path(directory) / "events.txt"
path.write_text("login\nview\nlogout\n", encoding="utf-8")
count = 0
with path.open(encoding="utf-8") as file:
for line in file:
if line.strip():
count += 1
print(count)
This keeps memory use bounded to the current line and the small counter. If the task specifically requires a list, append processed strings in the loop, but understand that the list will grow with the file; the loop alone does not make a collected list memory-efficient. Opening a missing path raises FileNotFoundError; choose a correct path or handle that error where the application can recover from it.
Split delimiter-separated text with split()
For a simple format with one known delimiter, read the text and call split() with that delimiter. The result is a list of strings, not numbers, and the delimiter is removed. This is suitable for predictable text such as colon-separated values, but it does not handle CSV quoting or delimiters embedded inside quoted fields.
from pathlib import Path
from tempfile import TemporaryDirectory
with TemporaryDirectory() as directory:
path = Path(directory) / "colors.txt"
path.write_text("red|green|blue", encoding="utf-8")
with path.open(encoding="utf-8") as file:
colors = file.read().split("|")
print(colors)
Whitespace is preserved unless you explicitly trim each field. For example, use a list comprehension with field.strip() if surrounding spaces are formatting rather than data.
Read delimited rows with csv.reader()
For CSV or similar delimited data, use Python’s csv module so quoted delimiters and escaped quotes are parsed according to the format. csv.reader() yields one list of strings per row. Convert the iterator to a list only when all rows should be held in memory; iterating directly also works for large files.
import csv
from pathlib import Path
from tempfile import TemporaryDirectory
with TemporaryDirectory() as directory:
path = Path(directory) / "people.csv"
path.write_text('name,city\n"Doe, Jane",Paris\nAlex,Rome\n', encoding="utf-8")
with path.open(newline="", encoding="utf-8") as file:
rows = list(csv.reader(file))
print(rows)
Each row remains separate, including a header when the input has one. If the caller needs only data records, skip or process the header explicitly; indexing the first row is not a way to read a multirow file. Use newline="" as recommended by the CSV module when opening a CSV text file.
Load numeric data with NumPy
For rectangular numeric text data, NumPy’s loadtxt() can parse values into an array. NumPy is an additional dependency. Its result is an array rather than a Python list; call tolist() when ordinary nested lists are required. This example assumes a consistently delimited numeric file without a header.
from pathlib import Path
from tempfile import TemporaryDirectory
try:
import numpy as np
except ImportError:
print("Install NumPy to run this example.")
else:
with TemporaryDirectory() as directory:
path = Path(directory) / "measurements.csv"
path.write_text("1.5,2.0\n3.5,4.0\n", encoding="utf-8")
values = np.loadtxt(path, delimiter=",")
print(values.tolist())
Use csv.reader() instead when rows may have different lengths, contain text, or require CSV quoting rules. Choose NumPy when the data is numeric and array operations are useful; its parser and array representation are different from a list of line strings.