def formPMStrgetOldName(PMStr): rootPath = r'D:\x\s\d\d' dirs = os.listdir(rootPath) for i in dirs: # 循环读取路径下的文件并筛选输出 if os.path.splitext(i)[1] == ".csv" and (PMStr in i): # 筛选csv文件 return i