with open("note.txt", "w", encoding="utf-8") as file:
file.write("Remember this")Line-by-line explanation
- Open selects note.txt in write mode.
- The with block will close it safely.
writesaves the string.- Existing content would be replaced.
with open("note.txt", "a", encoding="utf-8") as file:
file.write("\nAnother line")Line-by-line explanation
- Append mode preserves earlier text.
\nstarts a new line.- The new text is added at the end.