import os from pathlib import Path from itertools import chain import sys # conf target_path = '../户口本/0' #suffix = sys.argv[2] suffix = 'jpg' if len(sys.argv) != 3 else sys.argv[2] def get_range(n): len_n = len(str(n)) for num in range(1, n + 1): output = str(num) while len(output) < len_n: output = f'0{output}' yield output file_paths = list(chain(*[Path(target_path).rglob(f'*.{ext}') for ext in ['jpeg', 'jpg', 'png', 'JPG', 'PNG']])) num = len(file_paths) file_name_list = list(get_range(num)) for i in range(num): file = file_paths[i] print(file) new = file.parent / f'{file_name_list[i]}_img.{suffix}' file.rename(new)