suffix.py 844 B

1234567891011121314151617181920212223242526272829303132
  1. import os
  2. from pathlib import Path
  3. from itertools import chain
  4. import sys
  5. # conf
  6. <<<<<<< HEAD
  7. target_path = '/Users/sxkj/to_md/9.29/1'
  8. =======
  9. target_path = './HR_OCR/to_md/example/img'
  10. >>>>>>> a0b1c051142a2687d8ae2c63f543f021d3239f1f
  11. #suffix = sys.argv[2]
  12. suffix = 'jpg' if len(sys.argv) != 3 else sys.argv[2]
  13. def get_range(n):
  14. len_n = len(str(n))
  15. for num in range(1, n + 1):
  16. output = str(num)
  17. while len(output) < len_n:
  18. output = f'0{output}'
  19. yield output
  20. file_paths = list(chain(*[Path(target_path).rglob(f'*.{ext}') for ext in ['jpeg', 'jpg', 'png', 'JPG', 'PNG']]))
  21. print(len(file_paths))
  22. num = len(file_paths)
  23. file_name_list = list(get_range(num))
  24. for i in range(num):
  25. file = file_paths[i]
  26. print(file)
  27. new = file.parent / f'{file_name_list[i]}_img.{suffix}'
  28. file.rename(new)