suffix.py 698 B

123456789101112131415161718192021222324252627
  1. import os
  2. from pathlib import Path
  3. from itertools import chain
  4. import sys
  5. # conf
  6. target_path = '../户口本/0'
  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. num = len(file_paths)
  18. file_name_list = list(get_range(num))
  19. for i in range(num):
  20. file = file_paths[i]
  21. print(file)
  22. new = file.parent / f'{file_name_list[i]}_img.{suffix}'
  23. file.rename(new)