-In file handling, for inserting a data inside the file, we have a two mode in which we open the file
'w' open file in write mode.
'a' open file in append mode.
- Both mode write the content in the file, if file not exist then they create that file.
Difference-
- for a existing file,
- write mode 'w' erase the previous data of the file and insert new data while append mode don't erase previous data, it append new data with previous one.
program-
#open file in read mode
f=open("myfile.txt",'r')
print(f.read())
f.close()
This is previous data
#open file in read mode
f=open("myfile.txt",'w')
f.write("This is new data")
f.close()
f=open("myfile.txt",'r')
print(f.read())
f.close()
This is new data
#open file in read mode
f=open("myfile.txt",'a')
f.write("This is new datan")
f.close()
f=open("myfile.txt",'r')
print(f.read())
f.close()
This is previous data
This is new data