1

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.

RJS
  • 139
  • 1
  • 13
  • Maybe you should have a look at http://stackoverflow.com/questions/6648493/open-file-for-both-reading-and-writing – Marvo Dec 18 '16 at 19:06
  • *What* error message? Give a [mcve]. – jonrsharpe Dec 18 '16 at 19:07
  • To **read and write** a file you should open it with `'r+'` not `'r'` – jadsq Dec 18 '16 at 19:09
  • The question was marked as an exact duplicate of an existing question. I had searched on stackoverflow before posting the question and did not find a post that appeared to be a solution. Please provide a link to the duplicate. – RJS Dec 19 '16 at 12:47
  • The error message is in the posting...io – RJS Dec 19 '16 at 12:48

0 Answers0