-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathformat_json.py
More file actions
78 lines (62 loc) · 1.99 KB
/
Copy pathformat_json.py
File metadata and controls
78 lines (62 loc) · 1.99 KB
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import json
from collections import OrderedDict
import glob
import os
import sys
def recurse_subdirs(subdir=""):
global job_pool
if (subdir[-1] != "/"):
subdir += "/"
if (subdir == ""):
return
if os.path.exists(subdir):
#debug_print("DEBUG: searching subdir: " + subdir + "...")
if os.path.isdir(subdir) == False:
print "ERROR: not a directory: " + subdir
return
for filename in os.listdir(subdir):
if os.path.isdir(subdir + filename):
# recurse down and do check below
recurse_subdirs(subdir + filename + "/")
continue
# if filename is supported, add fullpath to job pool
if (".json" in filename.lower()):
job_pool.append(subdir + filename)
else:
print "ERROR: subdir not found: " + subdir
return job_pool
def ReformatFile(filename):
with open(filename, 'r') as in_file:
json_data = json.load(in_file, object_pairs_hook=OrderedDict)
# DEBUGGING
# print(json.dumps(json_data, indent=4, sort_keys=False))
# filename = filename.replace(".json", "2.json")
with open(filename, 'w') as out_file:
json.dump(json_data, out_file, indent=4,)
return
def main(arg):
# glob all files recursively into list
global job_pool
job_pool = list()
job_pool = recurse_subdirs(arg)
if job_pool is None:
print "job_pool is None"
return
# process list
count = 0
for filename in job_pool:
# print(filename)
ReformatFile(filename)
count += 1
if (count % 1000) == 0:
print "processed " + str(count) + " files..."
print "Script complete, processed " + str(count) + " files."
#arg = 'd:/dev/Morroblivion ESM2JSON/'
#arg = "./"
if len(sys.argv) == 1:
print "no command line argument"
else:
arg = sys.argv[1]
arg = os.path.normpath(arg).replace("\\", "/")
main(arg)
raw_input("Press ENTER to exit.")