<p>I want to do some basic filtering on a file. Read it, do processing, write it back. </p>

<p>I'm not looking for "golfing", but want the simplest and most elegant method to achieve this. I came up with:</p>

<pre><code>from __future__ import with_statement

filename = "..." # or sys.argv...

with open(filename) as f:
    new_txt = # ...some translation of f.read() 

open(filename, 'w').write(new_txt)
</code></pre>

<p>The <code>with</code> statement makes things shorter since I don't have to explicitly open and close the file.</p>

<p>Any other ideas ?</p>

This seems to work:
```
```
with open(filename, "r+") as f:
    new_txt = process(f.read())
    f.truncate(0)
    f.write(new_txt)
```
```