-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrename.py
56 lines (47 loc) · 1.59 KB
/
rename.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
56
import os
import shutil
import sys
import argparse
def main(args):
original_path = args.data_dir
original_path = original_path + "/"
saved_path = args.save_dir
saved_path = saved_path + "/"
make_path(saved_path)
all_folders = traversalDir_FirstDir(original_path)
for folder in all_folders:
files = os.listdir(original_path + folder)
i = 1
for file in files:
suffix = '.png'
name = folder + '_' + str(i).zfill(4) + suffix
i = i + 1
sub_saved_path = saved_path + folder
make_path(sub_saved_path)
shutil.copyfile(original_path + folder + '/' + file, sub_saved_path + '/' + name)
print("rename over")
# To get all sub folders in one folder
def traversalDir_FirstDir(path):
list = []
if (os.path.exists(path)):
files = os.listdir(path)
print("files",files)
for file in files:
m = os.path.join(path, file)
print("m",m)
if (os.path.isdir(m)):
h = os.path.split(m)
print("h",h)
list.append(h[1])
return list
# To judge whether a folder is existed.
def make_path(path):
if not os.path.exists(path):
os.makedirs(path)
def parse_arguments(argv):
parser = argparse.ArgumentParser()
parser.add_argument('data_dir', type=str, help='Directory with aligned images.')
parser.add_argument('save_dir', type=str, help='Directory to save renamed images.')
return parser.parse_args(argv)
if __name__ == '__main__':
main(parse_arguments(sys.argv[1:]))