suffix.py 770 B

12345678910111213141516171819202122232425262728
  1. import os
  2. from pathlib import Path
  3. from itertools import chain
  4. import sys
  5. # conf
  6. target_path = '/Users/zeke/Downloads/户口本测试样本1011-常住人口页'
  7. #suffix = sys.argv[2]
  8. suffix = 'jpg' if len(sys.argv) != 3 else sys.argv[2]
  9. def get_range(n):
  10. len_n = len(str(n))
  11. for num in range(1, n + 1):
  12. output = str(num)
  13. while len(output) < len_n:
  14. output = f'0{output}'
  15. yield output
  16. file_paths = list(chain(*[Path(target_path).rglob(f'*.{ext}') for ext in ['jpeg', 'jpg', 'png', 'JPG', 'PNG']]))
  17. print(len(file_paths))
  18. num = len(file_paths)
  19. file_name_list = list(get_range(num))
  20. for i in range(num):
  21. file = file_paths[i]
  22. print(file)
  23. new = file.parent / f'{file_name_list[i]}_img.{suffix}'
  24. file.rename(new)