-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrename_function.py
55 lines (45 loc) · 1.87 KB
/
rename_function.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
import os
def rename_files(src_path, dst_path, frmt, dataset=False, label=False,
checker=True):
""" This function rename a filename and save in a speficic directory.
=======
Params:
- src_path: Source File Path
- dst_path: Destination File path
- dataset: String input ('train' / 'test' / 'valid)'.
If None, the folder name of file path is used.
It's necessary pre structured the folders.
Ex. '../train/label01'
Dataset = 'train'
- label: String input. If None, the folder name of file path is used.
It's necessary pre structured the folders.
Ex. '../train/label01'
label = 'label01'
- checker: If True, only print the new filename
If False, apply the rename function
(create the files in dst_path)
"""
if not dataset:
dataset = src_path.split('/')[-2]
if not label:
label = src_path.split('/')[-1]
file_num = 0
total_num_files = len(str(len(os.listdir(src_path))))
num_digits = total_num_files - 1
for filename in os.listdir(src_path):
file_num += 1
num_zeros = '0' * (num_digits)
tot_str = len(str(num_zeros)) + len(str(file_num))
if tot_str > total_num_files:
num_digits -= 1
number_name = '0' * (num_digits) + str(file_num)
target_name = f'{number_name}_' + dataset + f'_{label}' + f'.{frmt}'
if checker:
print(f'Actual filename: {filename}')
print(f'Target name: {target_name} \n')
if checker == False:
src_path = src_path
old_name = src_path + '/' + filename
dst_path = dst_path
new_name = dst_path + '/' + target_name
os.rename(old_name, new_name)