Append Text to Existing File :
f = open("demofile2.txt", "a")
f.write("Now the file has more content!")
f.close()
f = open("demofile2.txt", "r")
print(f.read())
f.close()
Output :
Now the file has more content!
Overwrite File Content Using Write Mode :
f = open("demofile3.txt", "w")
f.write("Woops! I have deleted the content!")
f.close()
f = open("demofile3.txt", "r")
print(f.read())
f.close()
Output :
Woops! I have deleted the content!
Create New File Using Create Mode :
f = open("myfile.txt", "x")
f.close()
Output :
[myfile.txt created if it does not already exist]
Create File Using Write Mode :
f = open("myfile.txt", "w")
f.close()
Output :
[myfile.txt created or overwritten]