you have 2 possibilities:
- if you want to open the file just once, you should reset the
position of the stream with
fo.seek(0)
- you can close and reopen the file with
fo = open(docname, 'w')
The first option has one problem: if the replace-text is shorter than the original-text, some text will be left over at the end. To illustrate what I'm talking of: You have the text '12345' and want to replace '12' with 'a', then the resulting file would contain 'a3455'