If you want to replace the lines in the same file, you probably have to read its contents into a local variable, close it and open it again for writing:
In this example, I use the with statement , which closes the file after the with block completes - usually after the last command completes, or by exception.
def inplace_change(filename, old_string, new_string):
It is worth noting that if the file names were different, we could do it more elegantly with a single with statement.
Adam Matan Jul 09 '13 at 12:43 on 2013-07-09 12:43
source share