Thuta Learning
IntermediateProgrammingbeginner

Python Write/Create Files

Relax. We'll talk through this in plain words — no textbook voice.

To write into a file, we use mode "w" (overwrite) or "a" (append).

python
# "w" will overwrite the file
with open("log.txt", "w") as f:
    f.write("Log started.\n")

# "a" will append to the end
with open("log.txt", "a") as f:
    f.write("New entry added.")

# Read the final file to verify
with open("log.txt", "r") as f:
    print(f.read())
You should see
Log started. New entry added.
Python Write/Create Files | Thuta Learning