-
Notifications
You must be signed in to change notification settings - Fork 3
/
getJson.py
53 lines (45 loc) · 1.39 KB
/
getJson.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
#!/usr/bin/python
import os,sys
import json
def getDirsInDir(d, substr):
return [d for d in os.listdir(d) if os.path.isdir(d) and d.find(substr)!=-1]
def walkThrough(path, filelist, withPrefix=True):
if filelist==None:
filelist = []
files = os.listdir(path)
for f in files:
if os.path.isdir(path + f):
filelist = walkThrough(path + f + '/', filelist)
else:
filelist.append(path + f if withPrefix else f);
return filelist
def getFilesFullPathInDir(d):
if d[-1]!='/': d = d + '/'
filelist = []
filelist = walkThrough(d, filelist)
return filelist
def main():
dirs = getDirsInDir(".", "tutorial")
result = {}
for d in dirs:
result[d] = getFilesFullPathInDir(d)
json_str = json.dumps(result)
if len(sys.argv)<2:
print 'USAGE: ', sys.argv[0], ' output.json'
return
outfile = sys.argv[1]
if os.path.exists(outfile):
answer = raw_input("File " + outfile + " exists!!! Do you want to replace it ([Y]es/[N]o): ")
answer = answer.strip().lower()
if answer=="n" or answer=="no":
return
else:
print "Replacing file ", outfile
f = open(outfile,'w')
f.write(json_str)
else:
print "Writing to file ", outfile
f = open(outfile, 'w')
f.write(json_str)
if __name__ == '__main__':
main()