I have written the below code to replace the string "YANTRAPRODPATH" with "YantraProd1" in a file and save it:-
fileToModify = open("C:/workspace/PROD/bat1/customer_overrides.properties",'r+')
textToSearch = "<YANTRAPRODPATH>"
textToReplace = "YantraProd1"
f = fileinput.FileInput(fileToModify, inplace=True, backup='.bak')
for line in f:
print(line.replace(textToSearch, textToReplace))
f.close()
But I am getting below error:-
C:\workspace>python c:/workspace/ReplaceText.py
Traceback (most recent call last):
File "c:/workspace/ReplaceText.py", line 20, in <module>
for line in f:
File "C:\Python27\lib\fileinput.py", line 237, in next
line = self._readline()
File "C:\Python27\lib\fileinput.py", line 316, in _readline
os.rename(self._filename, self._backupfilename)
WindowsError: [Error 123] The filename, directory name, or volume label syntax is incorrect
Am I missing something here? And is there a more simpler way to do this?
c:/workspace/ReplaceText.pyf.__iter__()method was called (implicitly byfor line in f) and in this method in line 237 ofC:\Python27\lib\fileinput.pymethodself._readline()was called and in this method in line 316os.rename()function was called and inside this funcion exception of typeWindowsErrorwas raised.os.rename()is the most recent call but it was triggered byfor line f:via the chain of calls listed in the traceback.