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 potato potato potato
potato potato potato
potato potato potato potato potato potato potato potato potato
potato potato potato potato turnip potato
potato potato parsnip potato potato potato
I am trying to replace words inplace like so:
import fileinput
fileinput.close()
file = open('zzz.txt', "r+")
for line in fileinput.input(file, inplace=1):
print line.replace('turnip', 'potato')
I get the following error:
Traceback (most recent call last):
File "testss.py", line 19, in <module>
for line in fileinput.input(file, inplace=1):
File "c:\python27\lib\fileinput.py", line 253, in next
line = self.readline()
File "c:\python27\lib\fileinput.py", line 322, in readline
os.rename(self._filename, self._backupfilename)
WindowsError: [Error 123] The filename, directory name, or volume label syntax i
s incorrect
Am I doing something obviously wrong?