how to read then overwrite a file with python

 pythonCopywith open('myFolder/myfile.txt', "r") as myfile:
    data = myfilef.read()

with open('myFolder/myfile.txt', "w") as myfile:
    myfile.write(newData)

Here is what the above code is Doing:
1. Opening the file in read mode and storing the data in a variable.
2. Opening the file in write mode and overwriting the original data with newData.