how to replace (update) text in a file line by line
<p>I am trying to replace text in a text file by reading each line, testing it, then writing if it needs to be updated.  I DO NOT want to save as a new file, as my script already backs up the files first and operates on the backups.</p>

<p>Here is what I have so far...  I get fpath from os.walk() and I guarantee that the pathmatch var returns correctly:</p>

<pre><code>fpath = os.path.join(thisdir, filename)
with open(fpath, 'r+') as f:
    for line in f.readlines():
        if '&lt;a href="' in line:
            for test in filelist:
                pathmatch = file_match(line, test)
                    if pathmatch is not None: 
                        repstring = filelist[test] + pathmatch
                        print 'old line:', line
                        line = line.replace(test, repstring)
                        print 'new line:', line
                        f.write(line)
</code></pre>

<p>But what ends up happening is that I only get a few lines (updated correctly, mind you, but repeated from earlier in the file) corrected.  I think this is a scoping issue, afaict.</p>

<p>*Also: I would like to know how to only replace the text upon the first instance of the match, for ex., I don't want to match the display text, only the underlying href.</p>
