乱七八糟文件夹替换目标文件

import  os
import shutil
os.chdir(r'C:\Users\lenovo\Desktop\sjxallsvg')
goalfile=os.listdir(r'C:\Users\lenovo\Desktop\sjxallsvg')
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:\Users\lenovo\Desktop\sjxmovetrain')
posted @ 2020-10-30 17:05  diracy  阅读(93)  评论(0编辑  收藏  举报