How to read a TXT file in Python
To read a TXT file in Python, use open('file.txt', encoding='utf-8') in a with block and call .read(), .readline(), or .readlines() depending on whether you need the full content, one line, or all lines as a list. Specify an explicit encoding, handle FileNotFoundError and Unicode errors, and prefer with so files close automatically.
Basic file reading patterns
The most reliable way to read a text file is to open it with an explicit encoding and let Python manage resource cleanup using a with block. This prevents file handle leaks and ensures safe behavior across different platforms and Python versions.
Read entire file
Use read() when you want the full contents as a single string. Good for small files where memory usage is not a concern.
with open('data.txt', encoding='utf-8') as f:
text = f.read()
print(text)Read line by line
Use readline() to consume one line per call, which is useful when processing large files incrementally without loading everything into memory.
with open('data.txt', encoding='utf-8') as f:
line = f.readline()
while line:
print(line, end='')
line = f.readline()Read all lines into a list
readlines() returns a list where each item is a line including the trailing newline. Simple and clear when you need random access to lines.
with open('data.txt', encoding='utf-8') as f:
lines = f.readlines()
for line in lines:
print(line, end='')Iteration as a memory-efficient pattern
Iterating over a file object directly is often the best balance of readability and performance. It reads line by line without building an intermediate list, which is ideal for large logs or datasets.
with open('data.txt', encoding='utf-8') as f:
for line in f:
print(line, end='')This approach keeps memory usage low and works seamlessly with for loops, making it a common pattern in production codebases.
Handling encoding and platform differences
Text encoding impacts which byte sequences map to characters. UTF-8 is the safest default for cross-platform work, but some Windows files may use cp1252 or mbcs. Always specify encoding explicitly and handle errors intentionally.
with open('data.txt', encoding='utf-8', errors='replace') as f:
text = f.read()- Use
encoding='utf-8'for modern files and web content. - Use
errors='ignore'orerrors='replace'when input may contain invalid byte sequences. - On Windows, avoid relying on default system encoding; set it explicitly to prevent data corruption.
Common errors and defensive patterns
Defensive coding when reading files reduces crashes and makes troubleshooting easier. Anticipate missing files, permission problems, and encoding issues.
import os
try:
with open('data.txt', encoding='utf-8') as f:
text = f.read()
except FileNotFoundError:
print('File not found, check the path.')
except PermissionError:
print('Insufficient permissions to read the file.')
except UnicodeDecodeError as e:
print(f'Encoding issue: {e}')Comparison table: when to use each read method
| Method | Use case | Memory profile | Return type |
|---|---|---|---|
read() |
Small files, need full content as one string | High (entire file) | str |
readline() |
Process large files one line at a time, manual control | Low (one line) | str |
readlines() |
Small to medium files; need list of lines | Medium (list of lines) | list[str] |
| Iteration | Large files; clean, idiomatic line-by-line | Low (one line) | str (per iteration) |
Advanced considerations
For very large files, combine iteration with batch processing to avoid memory pressure. You can also use io.open directly for finer control, but the built-in open is sufficient for most tasks. When paths contain non-ASCII characters, use Python’s pathlib.Path.read_text(encoding='utf-8') for concise, cross‑platform code.
Choose the read pattern that matches your file size and access needs, always specify encoding, and handle predictable errors to keep file reading robust and portable.