trunck_files.py 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  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. new_name = re.sub(r'^d+.', '', file)
  9. if new_name != file:
  10. new_path = os.path.join(root, new_name)
  11. os.rename(old_path, new_path)
  12. print(f'Renamed: {old_path} -> {new_path}')
  13. def move_doc_to_ocr(directory):
  14. for root, dirs, files in os.walk(directory):
  15. for file in files:
  16. if file.endswith('.doc'):
  17. file_name = os.path.splitext(file)[0]
  18. folder_path = os.path.join(root, file_name)
  19. ocr_path = os.path.join(folder_path, 'ocr')
  20. if not os.path.exists(folder_path):
  21. os.makedirs(ocr_path)
  22. old_path = os.path.join(root, file)
  23. new_path = os.path.join(ocr_path, file)
  24. os.rename(old_path, new_path)
  25. print(f'Moved: {old_path} -> {new_path}')
  26. for root, dirs, files in os.walk(directory):
  27. for file in files:
  28. if file.endswith('.doc'):
  29. old_path = os.path.join(root, file)
  30. new_name = re.sub(r'^\d+\.', '', file)
  31. if new_name != file:
  32. new_path = os.path.join(root, new_name)
  33. os.rename(old_path, new_path)
  34. print(f'Renamed: {old_path} -> {new_path}')
  35. if __name__ == '__main__':
  36. directory = 'E:\\医学所有资料\\中华医学期刊数据库'
  37. #rename_doc_files(directory)
  38. move_doc_to_ocr(directory)