-
Notifications
You must be signed in to change notification settings - Fork 3
/
script.py
56 lines (42 loc) · 1.43 KB
/
script.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
import os
import sys
from os.path import abspath, basename, isdir, isfile
def tree(directory, padding = {}, indent = 0):
root = abspath(directory)
files = []
dirs = []
def get_branch(is_last):
return '%c-- ' % "|'"[ is_last and 1 or 0 ]
def get_padding():
return ''.join([ padding.get(level, '') for level in xrange(indent) ])
def update_padding():
return dict([ (idx, pad.replace("'", ' ').replace('--', ' ')) for idx, pad in padding.iteritems() ])
# find list of files and directores inside of the root
for name in os.listdir(root):
path = '%s%s%s' % (root, os.sep, name)
if isfile(path): files.append(path)
if isdir(path): dirs.append(path)
dir_count = len(dirs)
files_count = len(files)
# print current root
print ('%s%s') % (get_padding(), basename(root))
# print files from a current root directory
if files_count > 0:
padding = update_padding()
indent = indent + 1
for idx, entry in enumerate(files, 1):
padding[indent - 1] = get_branch(idx == files_count and dir_count == 0)
print ('%s%s') % (get_padding(), basename(entry))
indent = indent - 1
# recursively print directories
if dir_count > 0:
padding = update_padding()
indent = indent + 1
for idx, entry in enumerate(dirs, 1):
padding[indent - 1] = get_branch(idx == dir_count)
tree(entry, padding, indent)
def main():
directory = len(sys.argv) == 1 and os.getcwd() or sys.argv[1]
tree(directory)
if __name__ == '__main__':
main()