#!/usr/bin/python # Encoding: utf-8 # Author: Unavowed # License: GPLv3 (or any later version) import os, sys from stat import S_ISDIR def print_lines (lines): cnt = len (lines) last = not lines[cnt - 1] for x in xrange (len (lines)): if x + 1 == cnt: if last: sys.stdout.write ('╰') else: sys.stdout.write ('├') sys.stdout.write ('─╼ ') else: if lines[x]: sys.stdout.write ('│') else: sys.stdout.write (' ') sys.stdout.write (' ') lines = [] def escape (string): string = string.replace ('&', '&') string = string.replace ('<', '<') string = string.replace ('>', '>') string = string.replace ('"', '"') string = string.replace ('\'', ''') return string def print_dir (dir): global lines global path subdirs = [] if dir != path: print_lines (lines) dir = dir.replace (os.sep, '/') print '%s' \ % (escape (dir[len (path) + 1:]), dir[dir.rfind (os.sep) + 1:]) try: for ent in os.listdir (dir): name = dir + os.sep + ent try: st = os.lstat (name) if not S_ISDIR (st.st_mode) or ent.startswith ('.'): continue subdirs.append (name) except OSError: pass except OSError: pass subdirs.sort () for x in xrange (len (subdirs)): slast = (x + 1 == len (subdirs)) lines.append (not slast) print_dir (subdirs[x]) lines.pop () if len (sys.argv) > 1: path = sys.argv[1] else: path = '.' sys.stdout.write ('
╿\n')
print_dir (path)
sys.stdout.write ('
')