Python 脚本:自动分类文件夹并移动到指定位置
你可以使用以下的 Python 脚本来实现你的需求:\n\npython\nimport os\nimport re\nimport pandas as pd\nimport shutil\n\n# 指定目录路径和 Excel 文件路径\ndirectory = '/path/to/directory'\nexcel_file = '/path/to/excel/file.xlsx'\ncolumn_to_match = 0 # 要匹配的 Excel 列数\ncolumn_to_move = 1 # 要移动的 Excel 列数\n\n# 读取 Excel 文件\ndf = pd.read_excel(excel_file)\n\n# 遍历指定目录下的所有文件夹\nfor folder in os.listdir(directory):\n folder_path = os.path.join(directory, folder)\n if os.path.isdir(folder_path):\n # 提取文件夹名称中的数字和字母部分\n match = re.search(r'\w+', folder)\n if match:\n folder_name = match.group()\n # 在 Excel 中查找匹配项\n match_row = df[df.iloc[:, column_to_match] == folder_name]\n if not match_row.empty:\n # 获取分类值\n move_to_folder = match_row.iloc[0, column_to_move]\n # 创建目标文件夹\n target_folder_path = os.path.join(directory, move_to_folder)\n if not os.path.exists(target_folder_path):\n os.makedirs(target_folder_path)\n # 移动文件夹到目标文件夹\n shutil.move(folder_path, target_folder_path)\n\n\n请确保你已经安装了pandas库(使用pip install pandas进行安装)来处理 Excel 文件。请替换/path/to/directory和/path/to/excel/file.xlsx为你自己的目录路径和 Excel 文件路径。
原文地址: https://www.cveoy.top/t/topic/pBNO 著作权归作者所有。请勿转载和采集!