Use open() in text mode, specify the file’s encoding, and call read() inside a with block:
with open("file.txt", encoding="utf-8") as f:
text = f.read()
text is a Python str containing the file’s decoded contents. For a pathlib.Path, Path.read_text() is a shorter alternative.
Read the whole file into one string
The built-in open() defaults to read mode when you omit the mode argument. In text mode, f.read() with no size reads all remaining contents and returns them as a string:
with open("file.txt", encoding="utf-8") as f:
text = f.read()
print(text)
The with block closes the file when the block ends, even if an exception occurs. Specify the encoding that matches the file. For UTF-8 text, use encoding="utf-8"; Python’s tutorial recommends UTF-8 unless you know the file requires another encoding. An omitted encoding can depend on the platform and runtime configuration. Python’s input and output tutorial
#1 Best Overall
Use pathlib for a concise alternative
If you already have a Path object—or prefer the object-oriented filesystem API—call read_text():
from pathlib import Path
text = Path("file.txt").read_text(encoding="utf-8")
This returns the decoded file contents as a str and handles opening and closing the file internally. The pathlib reference
Rank #2
Choose between a string, streaming, and bytes
| Goal | Example | Result and trade-off |
|---|---|---|
Read an entire text file with open() |
with open("file.txt", encoding="utf-8") as f: text = f.read() |
A str; the whole file is read into memory. |
Read an entire text file with pathlib |
Path("file.txt").read_text(encoding="utf-8") |
A str; concise, but still reads the whole file. |
| Process a large text file incrementally | with open("file.txt", encoding="utf-8") as f: |
A str for each line; avoids retaining the entire file in one string. |
| Work with raw bytes or binary data | open("file.bin", "rb").read() or Path("file.bin").read_bytes() |
bytes; no text decoding is performed. |
Handle large files without loading them all at once
read() with no size consumes the remaining file contents, so a very large file can use more memory than your program can spare. Iterate over the file object to handle one line at a time:
with open("large.txt", encoding="utf-8") as f:
for line in f:
process(line)
Here, replace process(line) with the work your program needs to do. Avoid readlines() or list(f) when memory is the concern: both collect the lines rather than streaming them through your loop.
Use text mode only when the file contains text
Text mode decodes bytes into characters and translates platform-specific newline sequences to n by default. If you need exact raw bytes or are reading a JPEG, executable, or other binary file, use binary mode instead:
with open("image.jpg", "rb") as f:
data = f.read()
This gives you a bytes object, not a string. If the bytes represent text and you know the correct encoding, decode them deliberately. Binary mode is also the appropriate starting point when exact newline bytes must be preserved. Python’s tutorial explains text and binary I/O
Troubleshoot encoding errors
If the encoding you specify does not match the file, reading may raise UnicodeDecodeError. Check which encoding produced the file, then use that encoding in open() or read_text(). Silently ignoring decoding errors is not a general solution: it can discard or alter characters.
Python 3.12’s io documentation describes how omitted-encoding behavior can depend on UTF-8 Mode and how EncodingWarning can help locate uses of default encodings. Those details depend on the Python version and configuration; explicitly naming the expected encoding avoids relying on an unspecified default. Python 3.12’s io reference
Recommended Free Tools
Quick Recap
Best Value
Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.




