I have a folder with different subfolders in it. I have to iterate through all the files and check for the occurrences of John and Jose and replace with Mikel and Mourinho respectively.
This is the script I have written in Python. It is working fine but when I encounter a .gif
file it is giving me an error and it is not iterating further.
Can you please tell me why?
The error is
Traceback (most recent call last):
File "C:\Users\sid\Desktop\script.py", line 33, in <module>
os.chmod(path ,stat.S_IWRITE)
FileNotFoundError: [WinError 2] The system cannot find the file specified:'C:\Users\sid\Desktop\test\\images/ds_dataobject.gif.bak'
My code:
import os,stat
import fileinput
import sys
rootdir ='C:\Users\spemmara\Desktop\test'
searchTerms={"John":"Mikel", "Jose":"Mourinho"}
def replaceAll(file,searchExp,replaceExp):
for line in fileinput.input(file, inplace=1):
if searchExp in line:
line = line.replace(searchExp,replaceExp)
sys.stdout.write(line)
for subdir, dirs, files in os.walk(rootdir):
for file in files:
path=subdir+'/'+file
print(path)
os.chmod(path ,stat.S_IWRITE)
for key,value in searchTerms.items():
replaceAll(path,key,value)
os.chmod(path,stat.S_IREAD)