1# Read in the file
2with open('file.txt', 'r') as file :
3 filedata = file.read()
4
5# Replace the target string
6filedata = filedata.replace('ram', 'abcd')
7
8# Write the file out again
9with open('file.txt', 'w') as file:
10 file.write(filedata)
11