I have a Python script that writes some data to a file. I want to overwrite the the contents of the file with some new data.
When the file doesn't exist I create it and add data by using -
with open(path/to/myfile + '.json', 'w') as outfile:
json.dump(mydata, outfile)
Now there is some newdata
which should be written in place of mydata
inside myfile
.
If I understood correctly from some answers, using -
with open(path/to/myfile + '.json', 'w') as outfile:
json.dump(newdata, outfile)
deletes myfile
and creates a new one with newdata
as its contents.
Please correct me here if my understanding is wrong.
What I want to do is not delete the file and just overwrite the contents of the file (because I need to compare the last modified times of this file in another application). How can I do that?
EDIT: The duplicate link given answers how to append to an existing file. My objective is to overwrite the contents of the file entirely.