在文件夹中查找文件名并匹配Excel表格

我需要从给定的文件夹和子文件夹中提取文件名(图号)。 然后,我需要交叉引用find的graphics编号列表对照Excel文件,该文件包含图纸编号列表和相应的graphics说明。 输出需要是具有两列的Excel表格,用于图号和graphics说明。 在20个文件夹和子文件夹中有大约500个图纸需要遍历。

os模块walk ,可能会有所帮助,因为csv模块使excel可以读取文件。 没有更多的细节,我只能给你一个粗略的骨架。 在下面, root是包含您要search的所有目录的顶级目录:

 import os import csv #The below is sample code for reading your existing csv files. #It will vary based on their exact specifications with open('myfile.csv', newline='') as f: reader = csv.reader(f) d = {line[0]: line[1] for line in reader} #Next is code for opening the output file, #then going through all the filenames in our directory #For each filename, we look it up in the dictionary from earlier # then write that pair to the output file with open('output.csv', 'w+', newline='') as out: writer = csv.writer(out) for dirpath, dirnames, filenames in os.walk('root'): for filename in filenames: writer.writerow([filename, d[filename]) 

我build议你在官方的Python文档中查找csvos.walk