In this example we will show how to write to an existing file and overwrite any existing content using the open() function in Python.
Source Code
f = open('myfile3.txt', 'w')
f.write("Overwrite everything.")
f.close()
# Open and read the file after the overwriting:
f = open('myfile3.txt', 'r')
text = f.read()
print(text)
Output:
Overwrite everything.