Free tools Windows power users keep installed
One-click scans. No signup required.
Use open() in a with block, choose a mode, and call write(). For example, this writes a UTF-8 text file and replaces any existing contents:
text = "Hello, world!"
with open("output.txt", "w", encoding="utf-8") as file:
file.write(text)
Choose whether to overwrite or append
The mode determines what happens to existing file contents. Python’s Python 3.12 tutorial on reading and writing files documents these common choices:
As an Amazon Associate I earn from qualifying purchases.
| Mode | What it does | When to use it |
|---|---|---|
"w" |
Opens for writing and erases existing contents in a file with the same name. | Use when you intend to replace the file. |
"a" |
Opens for appending; new data goes at the end. | Use when you want to preserve existing contents and add more. |
"r" |
Opens for reading. It is the default if you omit the mode. | Not for writing. |
Replace the file
text = "New contents"
with open("output.txt", "w", encoding="utf-8") as file:
file.write(text)
Opening with "w" clears the old contents, so use it only when replacing them is intended.
Append to the file
with open("output.txt", "a", encoding="utf-8") as file:
file.write("Another linen")
write() does not add a line break automatically. Include n in the string when you want the next text to start on a new line.
#1 Best Overall
Why use a with block?
A with block closes the file when the block ends, including if an exception occurs. Python’s tutorial says it is good practice to use with for file objects and warns that without it—or an explicit close()—data passed to write() may not be fully written even if the program exits successfully. The examples here use the context manager so the file is closed reliably.
Set the text encoding explicitly
Text has to be encoded as bytes to be stored in a file. Python’s tutorial describes UTF-8 as the modern de facto standard and recommends specifying encoding="utf-8" unless another encoding is needed. If you omit the encoding, the default depends on the platform. In text mode, Python also translates newline characters to platform-specific line endings by default.
Rank #2
These examples are for text. For binary content such as images or executables, use binary mode rather than treating the data as a text string.
Use pathlib for a concise one-call write
Path.write_text() writes the supplied string in text mode and closes the file for you. It overwrites an existing file, so it is suitable when replacement is intended:
from pathlib import Path
Path("output.txt").write_text("Hello, world!", encoding="utf-8")
The official pathlib reference says the encoding parameter has the same meaning as it does for open(). Use open() when you need to choose append mode or perform several operations through a file object; use write_text() for a direct text write that should replace the file.
Writing structured data as JSON
For a plain string, call file.write(text). If a list or dictionary should be stored in JSON format, serialize it with json.dump() instead. The Python 3.12 tutorial’s JSON section notes that JSON files must use UTF-8:
Quick Recap
Best Value
import json
value = {"message": "Hello, world!"}
with open("output.json", "w", encoding="utf-8") as file:
json.dump(value, file)
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.
What’s actually slowing this PC down?
Pick the symptom - the matching free tool is one click away.




