import shutil,os,re new_path=r'C:\Users\lenovo\Desktop\groud_truth'#新文件夹路径 for derName, subfolders, filenames in os.walk(r'E:\water pool\test1\SaveExImages'):#要复制的文件夹所在的路径#print(derName)#print(filenames)#print(subfolders)for i inrange(len(filenames)):if filenames[i].endswith('.txt'):print(filenames[i].split('_')[4].split('.')[0])#filenames[i]为文件夹下的各文件名称,如ExImage_beam_data_183925_00301.txt,split('_')[4].split('.')[0]表获取第4个"_"和"."的字符串,此处为00301ifint(filenames[i].split('_')[4].split('.')[0])>300:#int:把字符串转化为整型数字file_path=derName+'\\'+filenames[i]newpath=new_path+'\\'+filenames[i]shutil.copy(file_path,newpath)