纯项目要求文件命名格式,真SB
import os,re
def getName(path):
# print(path)
file_list=os.listdir(path)
for file in file_list:
# print(file)
path2=path+'\\'+file
# print(path2)
file_list2=os.listdir(path2)
for file2 in file_list2:
# print(file2)
path3=path2+'\\'+file2
# print(path3)
file_list3=os.listdir(path3)
for file3 in file_list3:
# print(file3)
if file3.split('.')[1]=='tif':
# print(path3 +'\\'+file3)
p1=path3 +'\\'+file3
# print('p1=',p1)
# print(file3)
new_name = re.findall("\((.*?)\)",file3)
if new_name != []:
if new_name[0]=='必应':
new_name='biying'
else:
new_name=new_name[0]
# print('new_name=',new_name)
# p_2_1=path+'\\result\\'+file2+'\\'+new_name
# print('p_2_1=',p_2_1)
# print(p1)
p2=path+'\\result\\'+new_name+'\\'+new_name+'.tif'
# print('p2=',p2)
p2_1=path+'\\result\\'+new_name
# print('p2_1=',p2_1)
# print('p1=', p1)
os.makedirs(p2_1, exist_ok=True)
os.rename(p1,p2)
if __name__ == '__main__':
path=input('请输入要重命名的路径:')
getName(path)
# getName('E:\data\data5')