泸州百度做网站联系,漯河网做网站,手机网站宽度是多少,更好d wordpress今天客户需要 根据excel的文件名称这一列的内容#xff0c;找到电脑D盘的下所对应的文件位置#xff0c;要求用程序实现
数据样例#xff1a;记录.xlsx
解决代码#xff1a; 1、安装必要的库#xff1a;
pip install pandas openpyxl2、编写Python脚本#xff1a; im…今天客户需要 根据excel的文件名称这一列的内容找到电脑D盘的下所对应的文件位置要求用程序实现
数据样例记录.xlsx
解决代码 1、安装必要的库
pip install pandas openpyxl
2、编写Python脚本
import os
import pandas as pddef find_files_in_directory(file_names, search_path):file_locations {}for root, dirs, files in os.walk(search_path):for name in files:if name in file_names:file_locations[name] os.path.join(root, name)return file_locationsdef main():# 读取Excel文件excel_path 记录.xlsx # 替换为你的Excel文件路径df pd.read_excel(excel_path)# 假设文件名在名为 filename 的列中file_names df[文件名称].tolist()# 定义要搜索的目录search_path D:\\# 查找文件file_locations find_files_in_directory(file_names, search_path)# 输出结果for file_name, file_path in file_locations.items():print(f文件名: {file_name} 位于: {file_path})if __name__ __main__:main() 代码说明
导入必要的库os 用于遍历文件系统pandas 用于读取Excel文件。
定义函数 find_files_in_directory遍历指定目录及其子目录查找与给定文件名匹配的文件并记录其完整路径。
在 main 函数中
读取Excel文件获取文件名列表。
指定搜索目录D盘。
调用 find_files_in_directory 函数查找文件并输出结果。
你需要将 path_to_your_excel_file.xlsx 替换为实际的Excel文件路径并确保Excel文件中有一列包含文件名这里假设列名为 filename。运行脚本后程序会在D盘中查找这些文件并输出它们的路径。