I have a text file like this in names.txt:
My name is alex
My name is samuel
I want to just replace samuel with boxer
My code is :
#!/usr/bin/python
import re
f = open("names.txt",'r+')
for line in f:
if re.search(r'samuel',line,re.I):
print line
m=f.write(line.replace("samuel",'boxer'))
f.close()
even though Print line is printing the line correctly but replacement is not happening in names.txt. Let me know if anybody has any clue