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

时间:2016-10-13 16:15:57

标签: python excel windows

我需要从给定的文件夹和子文件夹中提取文件名(图号)。然后,我需要根据包含图纸编号列表和相应图纸描述的Excel文件交叉引用找到的图纸编号列表。输出需要是具有两列的Excel表,用于图纸编号和图纸描述。在20个文件夹和子文件夹中大约有500个图纸需要遍历。

1 个答案:

答案 0 :(得分:2)

来自walk模块的

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

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])

我建议您在官方Python文档中查找csvos.walk