How to read a text file in Python: methods and patterns
To read a text file in Python, use open(file_path, encoding="utf-8") in a with block and call .read(), .readline(), or .readlines() depending on your needs. Always specify an encoding, handle FileNotFoundError and Unicode errors, and prefer with so files close automatically. This guide covers the standard library approach, common pitfalls, and robust patterns for scripts and applications.
Core function: open
The built-in open() function returns a file object for reading text. Use a with statement to ensure timely resource release and automatic cleanup. The recommended default encoding for portability is UTF-8. Paths can be absolute strings or relative paths, and you can pass encoding=None to use the system default, though this is less portable.
Basic read pattern
The simplest way to read an entire text file into a string is to open it with a with block and call .read(). This is suitable for moderately sized files where loading content into memory is acceptable.
with open("notes.txt", encoding="utf-8") as f:
text = f.read()
print(text)Line-by-line patterns
Use .readline() to fetch one line at a time, or .readlines() to return a list of lines. For large files, iterate over the file object directly to keep memory use low while processing line by line.
# readline
with open("log.txt", encoding="utf-8") as f:
line = f.readline()
while line:
print(line, end="")
line = f.readline()
# readlines
with open("items.txt", encoding="utf-8") as f:
lines = f.readlines()
for line in lines:
print(line, end="")
# iteration (memory efficient)
with open("big.txt", encoding="utf-8") as f:
for line in f:
print(line, end="")Handling encodings and errors
Text files can use different character encodings. UTF-8 is common, but Windows-1252, Latin-1, and others appear in legacy data. Specify the correct encoding in open(). When decoding is uncertain, use errors="replace" or errors="ignore" to avoid crashes, and log issues for later review.
| Attribute | Verified Detail | Source Type |
|---|---|---|
| Recommended encoding | UTF-8 | Best practice / Python docs |
| Safe error handling | errors="replace" for detection | Python docs |
| Context manager | with | Python docs |
Working with paths
Provide the correct path to open(). Use absolute paths for clarity and relative paths for portability within projects. The pathlib module offers an object-oriented way to build paths; convert a Path object with Path.open() or str(Path).
from pathlib import Path
path = Path("data") / "inputs" / "config.txt"
with path.open(encoding="utf-8") as f:
content = f.read()
print(content)Robust error handling
Common issues include missing files, permission errors, and encoding mismatches. Catch FileNotFoundError when a file may be absent, PermissionError for access issues, and UnicodeDecodeError when the encoding is incorrect. Log or surface meaningful messages so you can diagnose problems quickly.
try:
with open("config.txt", encoding="utf-8") as f:
config = f.read()
except FileNotFoundError:
print("File not found: check the path.")
except UnicodeDecodeError:
print("Encoding error: try a different encoding.")
except OSError as e:
print(f"OS error: {e}")Performance and file size considerations
Reading very large files entirely into memory can exhaust RAM. For big datasets, iterate over the file object or read in chunks. Tools like io.TextIOWrapper let you control buffer sizes when needed. On modern systems, text I/O is usually fast; prioritize correctness and clarity before optimizing.
Advanced scenarios
When files contain structured text (CSV, JSON lines), use dedicated parsers instead of manual line splitting. For custom formats, combine iteration with .strip() and .split() to parse safely. Benchmark when in doubt, and keep error handling explicit so malformed input is reported clearly.
- Small configs:
open(..., encoding="utf-8").read() - Logs or line-based data: iterate with
for line in open(..., encoding="utf-8") - Large files: stream with
for line in open(..., encoding="utf-8")or chunked reads - Structured text: use
csvorjsonmodules
Reading text files reliably in Python means choosing the right open mode, specifying encoding, using context managers, handling expected errors, and matching the reading pattern to file size and structure. These practices keep your scripts robust across environments and data sources.