import os import re def rename_doc_files(directory): for root, dirs, files in os.walk(directory): for file in files: if file.endswith('.doc'): old_path = os.path.join(root, file) #文件名前面的数字及.去掉 new_name = re.sub(r'^\d+\.', '', file).strip() if new_name != file: new_path = os.path.join(root, new_name) os.rename(old_path, new_path) print(f'Renamed: {old_path} -> {new_path}') def move_doc_to_ocr(directory): for root, dirs, files in os.walk(directory): for file in files: if file.endswith('.doc'): file_name = os.path.splitext(file)[0] folder_path = os.path.join(root, file_name) ocr_path = os.path.join(folder_path, 'ocr') if not os.path.exists(folder_path): os.makedirs(ocr_path) old_path = os.path.join(root, file) new_path = os.path.join(ocr_path, file) os.rename(old_path, new_path) print(f'Moved: {old_path} -> {new_path}') for root, dirs, files in os.walk(directory): for file in files: if file.endswith('.doc'): old_path = os.path.join(root, file) new_name = re.sub(r'^\d+\.', '', file) if new_name != file: new_path = os.path.join(root, new_name) os.rename(old_path, new_path) print(f'Renamed: {old_path} -> {new_path}') if __name__ == '__main__': directory = 'E:\急诊科资料\中华医学期刊数据库' #rename_doc_files(directory) move_doc_to_ocr(directory)