How to search and replace in a file using python
<p>I have a text file like this in names.txt:</p>

<pre><code>My name is alex
My name is samuel
</code></pre>

<p>I want to just replace samuel with boxer</p>

<p>My code is :</p>

<pre><code>#!/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()
</code></pre>

<p>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</p>
