01 / 07
Pass a mode when you write
Every open so far has only read. In fact open takes a second argument called the mode, and leaving it out gave you "r" (read).
When you want to write, pass "w" (write). Once the file is open, write puts characters into it. And with still closes the file for you on the way out — exactly as it did for reading.
with open("memo.txt", "w") as f: f.write("morning")print("done writing")Result
done writing