1234567891011121314151617181920212223242526272829 |
- import os
- from pathlib import Path
- from itertools import chain
- import sys
- # conf
- target_path = './HR_OCR/to_md/example/img'
- #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)
|