trunck_files.py 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. import os
  2. import re
  3. def rename_doc_files(directory):
  4. for root, dirs, files in os.walk(directory):
  5. for file in files:
  6. if file.endswith('.doc'):
  7. old_path = os.path.join(root, file)
  8. #文件名前面的数字及.去掉
  9. new_name = re.sub(r'^\d+\.', '', file).strip()
  10. if new_name != file:
  11. new_path = os.path.join(root, new_name)
  12. os.rename(old_path, new_path)
  13. print(f'Renamed: {old_path} -> {new_path}')
  14. def move_doc_to_ocr(directory):
  15. for root, dirs, files in os.walk(directory):
  16. for file in files:
  17. if file.endswith('.doc'):
  18. file_name = os.path.splitext(file)[0]
  19. folder_path = os.path.join(root, file_name)
  20. ocr_path = os.path.join(folder_path, 'ocr')
  21. if not os.path.exists(folder_path):
  22. os.makedirs(ocr_path)
  23. old_path = os.path.join(root, file)
  24. new_path = os.path.join(ocr_path, file)
  25. os.rename(old_path, new_path)
  26. print(f'Moved: {old_path} -> {new_path}')
  27. for root, dirs, files in os.walk(directory):
  28. for file in files:
  29. if file.endswith('.doc'):
  30. old_path = os.path.join(root, file)
  31. new_name = re.sub(r'^\d+\.', '', file)
  32. if new_name != file:
  33. new_path = os.path.join(root, new_name)
  34. os.rename(old_path, new_path)
  35. print(f'Renamed: {old_path} -> {new_path}')
  36. if __name__ == '__main__':
  37. directory = 'E:\急诊科资料\中华医学期刊数据库'
  38. #rename_doc_files(directory)
  39. move_doc_to_ocr(directory)