import sqlite3 import os target_dirs = ['21762830', '21762848', '13011134', '21284967', '21316608', '11920416'] def rename_file(file_path, source_name, target_name, counter): os.rename(file_path + '/' + source_name, file_path + '/' + target_name) print(f'[{counter}] 目录 {file_path} 的文件 {source_name} 改名为 {target_name}') db_conn = sqlite3.connect('F:/data.dtu.dk/scripts/name_dict.db') db_cursor = db_conn.cursor() for dir in target_dirs: counter = 0 file_name_mapping = {} SQL_SELECT = f"SELECT dl_name, full_name FROM name_dict WHERE root_path = '{dir}';" db_cursor.execute(SQL_SELECT) exe_result = db_cursor.fetchall() for _result in exe_result: dl_name = _result[0] full_name = _result[1] file_name_mapping[dl_name] = full_name target_path = os.path.join('F:/data.dtu.dk', dir) file_list = os.listdir(target_path) for _file in file_list: counter += 1 if _file in file_name_mapping: rename_file(target_path, _file, file_name_mapping[_file], counter) db_cursor.close() db_conn.close()