Eclectic Media Git ptree / d868cae
Initial Commit Ariana Giroux 1 year, 10 months ago
1 changed file(s) with 62 addition(s) and 0 deletion(s). Raw diff Collapse all Expand all
0 #! /usr/bin/env python
1
2 # tree.py
3 #
4 # Written by Doug Dahms
5 #
6 # Prints the tree structure for the path specified on the command line
7
8 from os import listdir, sep
9 from os.path import abspath, basename, isdir
10 from sys import argv
11
12 def tree(dir, padding, print_files=False):
13 print(padding[:-1] + '+-' + basename(abspath(dir)) + '/')
14 padding = padding + ' '
15 files = []
16 if print_files:
17 files = listdir(dir)
18 else:
19 files = [x for x in listdir(dir) if isdir(dir + sep + x)]
20 count = 0
21 for file in files:
22 count += 1
23 print(padding + '|')
24 path = dir + sep + file
25 if isdir(path):
26 if count == len(files):
27 tree(path, padding + ' ', print_files)
28 else:
29 tree(path, padding + '|', print_files)
30 else:
31 print(padding + '+-' + file)
32
33 def usage():
34 return '''Usage: %s [-f] <PATH>
35 Print tree structure of path specified.
36 Options:
37 -f Print files as well as directories
38 PATH Path to process''' % basename(argv[0])
39
40 def main():
41 if len(argv) == 1:
42 print(usage())
43 elif len(argv) == 2:
44 # print just directories
45 path = argv[1]
46 if isdir(path):
47 tree(path, ' ')
48 else:
49 print('ERROR: \'' + path + '\' is not a directory')
50 elif len(argv) == 3 and argv[1] == '-f':
51 # print directories and files
52 path = argv[2]
53 if isdir(path):
54 tree(path, ' ', True)
55 else:
56 print('ERROR: \'' + path + '\' is not a directory')
57 else:
58 print(usage())
59
60 if __name__ == '__main__':
61 main()