import os from pathlib import Path from itertools import chain import sys # conf <<<<<<< HEAD target_path = '/Users/sxkj/to_md/9.29/1' ======= target_path = './HR_OCR/to_md/example/img' >>>>>>> a0b1c051142a2687d8ae2c63f543f021d3239f1f #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']])) print(len(file_paths)) 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)