🚀 Supercharge your YouTube channel's growth with AI.
Try YTGrowAI FreePython Print to File: Save Script Output

To save Python print() output in a file, send it to an open text file instead of the terminal. The file argument lets print() write its text to that file.
This article shows how to write print() output to a UTF-8 text file and choose whether to replace or add to its contents.
TL;DR: Write Python Print Output to a File
Pass an open text file as file= to send print() output there. A with block closes the file when writing ends.
- Use w to replace file contents or a to add lines.
- Set encoding=”utf-8″ for predictable text encoding.
- Choose print(file=…) for selected messages. Redirect sys.stdout only when all print calls should move.
What Does Printing to a File Mean in Python?
print() formats values as text and writes them to a writable text stream. By default that stream is sys.stdout, usually the terminal. Its file= argument selects another stream, such as the file object returned by open().
The sep argument separates multiple values with a space by default, while end adds a newline after each call. The official print() documentation describes these arguments and the writable-stream requirement. I use file= below because it sends only the chosen calls to the file.
How to Write Print Output to a File Step by Step
Open a text file in a context manager, pass its handle to print(), and read the saved text back. I use a relative path, so Python resolves it from the directory where the script runs.
Step 1: Open the destination in text mode
Use encoding=”utf-8″ when writing text that should have an explicit encoding. Mode w creates the file or replaces its existing contents.
Step 2: Pass the handle to print()
Save this as print_to_file_demo.py and run it with python3 print_to_file_demo.py. The script reads the saved text and displays it in the terminal.
from pathlib import Path
output_path = Path("print-output.txt")
with output_path.open("w", encoding="utf-8") as output_file:
print("Run started", file=output_file)
print("Rows processed:", 3, file=output_file)
with output_path.open(encoding="utf-8") as output_file:
print(output_file.read(), end="")
The executed script printed:
Run started Rows processed: 3

Both print() calls write to the open file, and leaving the with block closes it. The read-back shows the newline added by print() after each call.
Which File Mode and Output Method Should You Use?
Choose a file mode according to what should happen to existing contents. For selected print calls, file= is the narrowest choice because it leaves unrelated output in the terminal.
| Mode | Effect | Use it when |
|---|---|---|
| w | Creates or truncates the file | Each run should make a fresh report |
| a | Creates if missing and writes at the end | Earlier output must remain |
| x | Creates only, otherwise raises FileExistsError | An existing file must not be replaced |
Use write() when you already have one complete string and control its line endings. Unlike print(), write() does not add a separator or newline, and it accepts strings rather than converting values. For several values that need print() spacing, keep print(file=…).
When existing print calls cannot be edited, contextlib.redirect_stdout(file) redirects their standard output for the duration of a with block, then restores the previous stream. It affects all ordinary print calls in that block that use sys.stdout, not just one selected call.
Shell redirection captures a whole program’s standard output without changing its Python source:
python3 script.py > output.txt
The > operator replaces the destination, while >> appends. Standard error stays separate unless redirected too. Choose this route when all standard output belongs in one file, not when the program needs to choose destinations per call.
For text encoding, specify encoding=”utf-8″ when opening the file and use the same encoding when reading it. Binary data such as image bytes needs a binary file and write(), not print(). For application records that need severity levels or multiple destinations, use AskPython’s Python logging guide.
Common Errors When Printing to a File
Match the open mode and path to the operation. These checks cover common failures without changing how print() formats values.
| Case | Symptom | Fix |
|---|---|---|
| File opened with r | Writing raises an error | Choose w, a, or x according to the table above |
| File opened with wb | print() cannot write text to the binary stream | Use text mode for print(), or write bytes with a binary handle |
| Unexpected relative path | File appears outside the script’s folder | Check Path.cwd(). Relative paths use the current working directory |
| Extra blank lines | Printed lines have a blank line between them | When a string already ends in a newline, use print(value, end=””) |
Check Path.cwd() when the file appears outside the script’s folder. Relative paths use the current working directory, not necessarily the script directory.
Conclusion: Use a File Argument for Direct Output
Open a text file with the mode and encoding you need, then pass it as file= to print(). Use stdout redirection for a group of existing calls and shell redirection for a whole process.
See the Python documentation for print() and open(), or read AskPython’s standard library overview.
FAQ
These answers cover two related file-output choices.
How do I save every print() call to a file?
Use contextlib.redirect_stdout(file) around the code that makes those calls. Prefer file= when you can change individual calls.
Does open(…, “a”) create the file?
Yes. Append mode creates a missing file and writes new output at its end, preserving existing contents.


