#!/usr/bin/env python3 import sys import os # Line string line = '|-- ' green = '\033[32m' blue = '\033[34m' color_reset = '\033[0m' tab = ' ' def tree(path, depth): # use list comprehension to ignore files begining with . files = [os.path.join(path,f) for f in os.listdir(path) if not f.startswith('.')] # Sort our files in ascending order files.sort() for f in files: # print(f, s) if os.path.isdir(f): print(tab * depth, line , blue, f.replace(path+'/', ''), color_reset, sep='') tree(f, depth+1) elif os.path.isfile(f): print(tab * depth, line, f.replace(path+'/', ''), sep='') # Allow the user to specify a directory to list the contents of if __name__ == '__main__': path = '.' # Handle arguments if len(sys.argv) > 1: # The path should be the only argument not starting with a - for a in sys.argv[1:]: if not a.startswith('-'): path = a # If the directory does not exist quit if not os.path.isdir(path): print('Directory does not exist.') exit() # Break since we should have found the path break print(blue, path, color_reset, sep='') tree(path, 0)