Zest.

Python: Write a file

A short, tested Python example for write a file. Copy it and adjust the names and paths for your project.

Python
with open("out.txt", "w", encoding="utf-8") as f:
    f.write("hello\n")

# "a" appends instead of overwriting
with open("out.txt", "a", encoding="utf-8") as f:
    f.write("more\n")

Find it in Zest

Search for “python write a file” in Zest and this example appears above the results, with other matches to choose from. Searches stay private.

More Python examples

All Python examples