I have a directory with a number of files in it. I want to walk through the directory, oepn each file, replace a specific word with the same word but in Upper Case and then save the file. I don’t want to change the file name, just save it with the changed word.
The script I wrote to do this is below. The result is an error message
finf.write(FileinA)
io.UnsupportedOperation:not writable
Please advise what to do to get the desired result.
for dirName, subdirList, fileList in os.walk (sourcedir):
for filename in fileList:
with open (filename,"r+", encoding="ascii", errors ="surrogateescape") as fin:
FileinA=(fin.read())
FileinA = FileinA.replace('think','THINK')
fin.seek(0)
fin.write(FileinA)
fin.truncate()
The error message is in the posting...fin.write(FileinA)
io.UnsupportedOperation:not writable I edited the script to include "r+" and fin.seek(0) and fin.truncate(0) the result is an error message Errno13 Permission denied ...which refers to the first file in the directory to be walked.