-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathlistdir.py
More file actions
37 lines (29 loc) · 779 Bytes
/
Copy pathlistdir.py
File metadata and controls
37 lines (29 loc) · 779 Bytes
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
#!/usr/bin/env python
import os
import errno
def path_hierarchy(path):
hierarchy = {
'type': 'folder',
'name': os.path.basename(path),
'path': path,
}
try:
hierarchy['children'] = [
path_hierarchy(os.path.join(path, contents))
for contents in os.listdir(path)
]
except OSError as e:
if e.errno != errno.ENOTDIR:
raise
hierarchy['type'] = 'file'
return hierarchy
if __name__ == '__main__':
import json
import sys
try:
directory = sys.argv[1]
except IndexError:
directory = "."
json_obj = json.dumps(path_hierarchy(directory), indent=2, sort_keys=True)
with open('src/code_list.json', 'w') as f:
f.write(json_obj)