Skip to content Skip to sidebar Skip to footer

Inplace File String Replacement - Windows Error

I am trying to implement the answer given here: https://stackoverflow.com/a/1388570/1461850 I have an input file zzz.txt containing: potato potato potato potato potato potato potat

Solution 1:

Instead of passing the file object, pass the file name(or list of filenames) to fileinput.input:

fileinput.input('zzz.txt', inplace=1):

Note: Don't use file as a variable name, file is a built-in function.

Solution 2:

What about:

import fileinput
a = 'zzz.txt'for line in fileinput.input(a, inplace=1):
    print line.replace('turnip', 'potato')

Post a Comment for "Inplace File String Replacement - Windows Error"