示例:python根据文件名查找文件路径名
程序员文章站
2024-03-14 14:29:22
...
示例:根据文件名查找文件路径名,并重新输出为excel
利用pandas读取excel文件,寻找对应文件的存放路径
import pandas as pd
import os
def picture_handle():
pic_namepath = {}
for path, dirs, files in os.walk('./目标文件夹'):
if files:
# print(path, dirs, files)
for name in files:
pic_name = name.split('.')[0]
pic_path = os.path.join(path, name)
pic_namepath[pic_name] = pic_path
return pic_namepath
# print(pic_namepath)
def file_matching(picture_namepath):
is_exsited = []
xlsx = pd.ExcelFile('./读取excel.xlsx')
df = pd.read_excel(xlsx)
xlsx_bundle_name = df['数据名称']
for bundle_name in xlsx_bundle_name:
if bundle_name in picture_namepath:
is_exsited.append((picture_namepath[bundle_name]))
else:
is_exsited.append(False)
col_name = df.columns.tolist()
col_name.append('对应文件路径')
df = df.reindex(columns=col_name)
df['对应文件路径'] = is_exsited
df.to_excel('./文件名对应的路径名.xlsx', sheet_name='Sheet1')
if __name__ == "__main__":
handle = picture_handle()
file_matching(handle)