- 1
Writing files in python
when I am using file.write(), y=the contents are getting deleted, can we get that it doesn't delete the original contents but writes after the original contents.
4 Respostas
+ 5
You would need to open the file in append mode.
+ 4
with open(filename, "a") as f:
f.write(content)
+ 4
I found this in the comments of the lesson:
To edit an existing file without losing previous data use append(a) instead of write(w)
for example :
file = open("newfile.txt", "a")
file.write("\nThis has been written again to a file")
file.close()