2.9.1.6 Writing to Files
Python Programming
To write to a file, open it in write mode (w) and use the write () method. To append data, use append mode (a).
python
# Writing to a file
with open("example.txt", "w") as file:
file.write("As-Salaam-Alaikum, World!\n")
# Appending to a file
with open("example.txt", "a") as file:
file.write("Appending new line.\n")Explanation: In the above code: