Append (a ) : Add data at the end of the file. Existing data retained
Write ( w) : Add data at the beginning of the file. Existing data overwritten
Create a file and Write to it
If any existing file is there , it will be overwritten. A new file will be created.
fob=open('data1.txt','w')
fob.write("this is a new text just written")
fob.close()
If we don't want any existing file to be overwritten then we need to change the mode of opening of the file.
fob=open('data1.txt','x')
fob.write("this is a new text just written")
fob.close()
Above code will give error as data1.txt file is already there.
Let us write and then read the content.
fob=open('data1.txt','w')
fob.write("this is a new text just written")
## Writting over , now read
fob=open('data1.txt','r')
print(fob.read())
fob.close()