Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

iTechGuides is reader-supported. When you buy through links on our site, we may earn an affiliate commission. As an Amazon Associate I earn from qualifying purchases. Learn more

Use Python’s built-in open() in a with block, choose "w" to replace the file or "a" to append, and call write() with your string:

text = "Hello, world!"

with open("output.txt", "w", encoding="utf-8") as file:
    file.write(text)

Specifying UTF-8 makes the text encoding explicit, and the context manager closes the file when the block ends.

Choose whether to overwrite or append

The mode you pass to open() determines what happens to existing content:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Mode What it does Use it when
"w" Opens the file for writing and erases its existing contents. You intend to replace the file.
"a" Opens the file for appending; new text goes at the end. You want to preserve existing content and add more.
"r" Opens the file for reading. This is the default mode if you omit the mode argument. You only need to read the file, not write to it.

For example, append a line by including the newline character in the string:

with open("output.txt", "a", encoding="utf-8") as file:
    file.write("Another linen")

write() writes the string you give it; it does not add a newline automatically. Add n yourself wherever a line break is needed. See the Python 3.12 tutorial on reading and writing files for the documented file modes and write behavior.

Why use a with block?

A with statement closes the file when its block finishes, including if an exception occurs while writing. Without a context manager or an explicit close(), data passed to write() may not be completely written even if the program exits successfully. Python’s tutorial calls using with good practice when dealing with file objects.

Use pathlib for a concise one-call write

If you do not need to work with a file object for several operations, Path.write_text() writes a string to a path and closes the file for you:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
from pathlib import Path

Path("output.txt").write_text("Hello, world!", encoding="utf-8")

This method overwrites an existing file. Its encoding parameter has the same meaning as the encoding argument to open(). See the Python pathlib reference.

Set an encoding and handle newlines

Text is stored as bytes, so Python encodes a string when writing it to a text file. The Python 3.12 tutorial describes UTF-8 as the modern de facto standard and recommends specifying encoding="utf-8" unless another encoding is required. If you omit the encoding, Python uses a platform-dependent default, which can make text behave differently across systems.

In text mode, Python translates newline characters to the platform’s line-ending format by default. That is generally suitable for text files. For binary content such as images or executables, use binary mode and bytes instead of writing a string as text.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Write structured data as JSON

For a plain string, use file.write(text). If you have a list or dictionary that should be stored as JSON, serialize the value with json.dump() instead:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
import json

settings = {"theme": "dark", "notifications": True}

with open("settings.json", "w", encoding="utf-8") as file:
    json.dump(settings, file)

The Python tutorial specifies UTF-8 for JSON files. Opening the file with encoding="utf-8" makes that choice explicit. Refer to the Python tutorial’s JSON section for structured-data serialization.

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.