-
Notifications
You must be signed in to change notification settings - Fork 13
/
update_file_names.py
64 lines (41 loc) · 1.52 KB
/
update_file_names.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
57
58
59
60
61
62
63
64
import os
import json
counter_png = 0
counter_json = 0
directory = "<path_to_directory>"
def listdir_nohidden(path):
for f in os.listdir(path):
if not f.startswith('.'):
yield f
list_of_files = sorted(listdir_nohidden(directory),
key=lambda x: int(os.path.splitext(x)[0]))
print("Updating images....")
for idx, file in enumerate(list_of_files):
is_png_file = os.path.splitext(file)[1] == ".png"
if(file == ".DS_Store"):
break
if(is_png_file):
f_new = str(counter_png) + '.png'
os.rename(os.path.join(directory, file),
os.path.join(directory, f_new))
counter_png += 1
print("Old png file " + file + " New png file: " + f_new)
print("Finished updating pngs...")
print("Updating json files....")
for idx, file in enumerate(list_of_files):
is_json_file = os.path.splitext(file)[1] == ".json"
if(file == ".DS_Store"):
break
if(is_json_file):
f_new = str(counter_json) + '.json'
counter_json += 1
with open(directory + file, "r") as jsonFile:
data = json.load(jsonFile)
data["image"] = os.path.splitext(f_new)[0] + '.png'
data["properties"]["files"][0]["uri"] = os.path.splitext(f_new)[
0] + '.png'
with open(directory + f_new, "w") as jsonFile:
json.dump(data, jsonFile, indent=4)
os.remove(os.path.join(directory, file))
print("Old json file " + file + " New json file: " + f_new)
print("All done!")