The following program displays the directory structure of a specified path using ASCII characters. The program can optionally display files in addition to directories. This program functions similar to the windows 'tree' command.
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 57 58 59 60 61 62 | #! /usr/bin/env python
# tree.py
#
# Written by Doug Dahms
#
# Prints the tree structure for the path specified on the command line
from os import listdir, sep
from os.path import abspath, basename, isdir
from sys import argv
def tree(dir, padding, print_files=False):
print padding[:-1] + '+-' + basename(abspath(dir)) + '/'
padding = padding + ' '
files = []
if print_files:
files = listdir(dir)
else:
files = [x for x in listdir(dir) if isdir(dir + sep + x)]
count = 0
for file in files:
count += 1
print padding + '|'
path = dir + sep + file
if isdir(path):
if count == len(files):
tree(path, padding + ' ', print_files)
else:
tree(path, padding + '|', print_files)
else:
print padding + '+-' + file
def usage():
return '''Usage: %s [-f] <PATH>
Print tree structure of path specified.
Options:
-f Print files as well as directories
PATH Path to process''' % basename(argv[0])
def main():
if len(argv) == 1:
print usage()
elif len(argv) == 2:
# print just directories
path = argv[1]
if isdir(path):
tree(path, ' ')
else:
print 'ERROR: \'' + path + '\' is not a directory'
elif len(argv) == 3 and argv[1] == '-f':
# print directories and files
path = argv[2]
if isdir(path):
tree(path, ' ', True)
else:
print 'ERROR: \'' + path + '\' is not a directory'
else:
print usage()
if __name__ == '__main__':
main()
|
For some of the projects I have worked on it is necessary to document the directory [and file] structure of a specified path. Manually creating a ASCII tree representation is laborious. In the windows world there exists the 'tree' command to handle this task. However in the Linux/Unix world I was not aware of an equivalent command. Thus I created this program.
There is a utility.... FYI:
Thanks... Oh darn. Looks like I did it again... reinventing the wheel. ;) Thanks for the info Keith.
More concise output and code. I find the following more concise (where find is available...).
No success.
No Problem. May I ask A Question, How to filter hidden file(s) and directory(s)? (You can test with set the path to home directory in linux) (Sorry for my poor english, i'm chinese student in Malaysia.)