Assume I have a file content.txt, with the content Ex nihilo nihil fit. I am willing to replace it with Ex nihilo nihil est. The code is:
with open("content.txt", "r+") as f:
content = f.read()
content = content.replace("fit", "est")
print(content)
f.write(content)
f.close()
After that, the content of the file becomes:
Ex nihilo nihil fit
Ex nihilo nihil est
Why? The only thing I need is Ex nihilo nihil est. What is the correct code?