-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfindtree.py
56 lines (34 loc) · 944 Bytes
/
findtree.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
"""
Visualize the file system structure
of the result of find command.
Usage example:
find . -name "*.ipynb" ! -name "*-checkpoint.ipynb" | python /path/to/findtree.py
"""
import fileinput
import re
from pprint import pprint
def insert(d, key):
if key not in d:
d[key] = {}
def fill(d, first, rest):
insert(d, first)
parent = d[first]
for el in rest:
insert(parent, el)
parent = parent[el]
def print_tree(d):
def p(prefix, d):
for k, v in d.items():
print('{}{}'.format(prefix, k))
p(prefix+'\t', v)
p('', d)
if __name__ == '__main__':
d = {}
for line in fileinput.input():
line = line[2:]
elements = line.split('/')
elements = list(map(lambda x: x.strip(), elements))
first = elements[0]
rest = elements[1:]
fill(d, first, rest)
print_tree(d)