import os
import shutil
os.chdir(r'C:UserslenovoDesktopsjxallsvg')
goalfile=os.listdir(r'C:UserslenovoDesktopsjxallsvg')
def replace(path):
namelist=os.listdir(path)
for name in namelist:
absolutepath=os.path.join(path,name)
if os.path.isfile(absolutepath):
for i in goalfile:
if i==os.path.basename(absolutepath):
print(absolutepath)
print(i)
print('*'*20)
newabsolutepath=absolutepath.rsplit('.')[0]+'_bak'+'.'+absolutepath.rsplit('.')[1]
shutil.copy2(i,newabsolutepath)
os.remove(absolutepath)
os.rename(newabsolutepath,absolutepath)
elif os.path.isdir(absolutepath):
replace(absolutepath)
replace( r'C:UserslenovoDesktopsjxmovetrain')