我正在尝试实现此处给出的答案:
https://stackoverflow.com/a/1388570/1461850
我有一个输入文件zzz.txt
,其中包含:
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
我正在尝试像这样替换单词:
import fileinput
fileinput.close()
file = open('zzz.txt', "r+")
for line in fileinput.input(file, inplace=1):
print line.replace('turnip', 'potato')
我收到以下错误:
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
我在做一些明显错误的事情吗?