Currently I'm using this:
f = open(filename, 'r+')
text = f.read()
text = re.sub('foobar', 'bar', text)
But the problem is that the old file is larger than the new file. So I end up with a new file that has a part of t...