For a text file that fits comfortably in memory, open it with a context manager and print the string returned by read():
with open("example.txt", encoding="utf-8") as f:
print(f.read())
For a large file, iterate over it line by line instead. The examples below explain how to choose the right method, avoid extra blank lines, and handle encoding and binary data.
Print a small or moderate text file
Use with open(...) to open the file and read() to get its contents as one string:
with open("example.txt", encoding="utf-8") as f:
print(f.read())
Replace example.txt with the file’s path. The with statement closes the file when the block ends, including if an exception occurs. Python’s tutorial recommends specifying UTF-8 unless you know the file needs another encoding; if you omit encoding, the default depends on the platform. See the Python 3.14 tutorial on input and output.
Free tools Windows power users keep installed
One-click scans. No signup required.
#1 Best Overall
Because read() without a size loads the entire decoded file into memory, this is best for files of a manageable size. For a large file, use the streaming approach below.
Print a large file line by line
Iterate over the file object to process one line at a time rather than loading the whole file at once:
Rank #2
with open("example.txt", encoding="utf-8") as f:
for line in f:
print(line, end="")
Each line may already include its line ending. Since print() adds a newline by default, end="" prevents an extra newline—and the blank lines it can create—while retaining the file’s existing line breaks. File iteration is described in the Python tutorial.
Use pathlib for concise whole-file reading
If you prefer a path-oriented API, Path.read_text() reads and returns the entire file as a string:
Outdated Drivers Are Slowing You Down
One free scan finds every outdated or missing driver and matches the right update for your exact hardware.Free scan · exact hardware matchWindows Errors? Fix Them Before They Spread
Repair common Windows errors and clear accumulated junk for a smoother, more stable PC - no reinstall needed.Free scan · no reinstallfrom pathlib import Path
print(Path("example.txt").read_text(encoding="utf-8"))
It closes the file after reading, but still holds the complete text in memory. Use it for reasonably sized text files, not as a streaming alternative. See the pathlib reference.
Choose the approach that fits the file
| Situation | Approach | Trade-off |
|---|---|---|
| Small or moderate text file | with open(path, encoding="utf-8") as f: print(f.read()) |
Direct, but reads the entire file into memory. |
| Large text file | Iterate over the file object and print each line with end="". |
Processes incrementally without adding extra line breaks. |
| Concise path-based whole-file read | Path(path).read_text(encoding="utf-8") |
Convenient, but returns the entire file as one string. |
| Binary data, such as an image or executable | Open in binary mode and handle bytes. | Text decoding and newline translation are not appropriate for arbitrary binary content. |
Handle encoding and binary data correctly
Text mode decodes file bytes into strings and, by default, translates platform-specific line endings. Pass the file’s actual encoding to open() or read_text(). UTF-8 is a recommended choice when it matches the file; if the file uses another encoding, specify that instead. The built-in open() reference documents the encoding and newline behavior.
For an image, executable, or other binary file, open it in binary mode, such as open("photo.jpg", "rb"), and process the resulting bytes with code suited to that format. Printing arbitrary bytes as text can produce decoding errors or meaningless output.
Quick Recap
Best Value
Fix common file-printing problems
- Memory use grows with a large file:
f.read()without a size reads the whole file. Iterate overfto process it line by line. - Extra blank lines appear: The line may already end in a newline, and
print()adds another. Useprint(line, end=""). - Text differs between computers or decoding fails: The default encoding is platform-dependent. Specify the file’s actual encoding explicitly.
- The input is an image or executable: Open it in binary mode and process bytes instead of trying to print it as decoded text.
- The file remains open longer than expected: Put the operation inside a
with open(...) as fblock so the file closes when the block ends.
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.

