comparison python/tree2.py @ 384:5ae5ada91ac8

simple numbering
author Jeff Hammel <jhammel@mozilla.com>
date Fri, 26 Jul 2013 09:45:46 -0700
parents 8d1ad56761b0
children 6ef0ea2f10e3
comparison
equal deleted inserted replaced
383:8d1ad56761b0 384:5ae5ada91ac8
21 break 21 break
22 return level 22 return level
23 23
24 def tree(directory): 24 def tree(directory):
25 25
26 sort_key=lambda x: x.lower()) 26 sort_key=lambda x: x.lower()
27 retval = [directory] 27 retval = [directory]
28 top = depth(directory) 28 top = depth(directory)
29 indent = [] 29 indent = []
30 last = {}
30 for dirpath, dirnames, filenames in os.walk(directory, topdown=True): 31 for dirpath, dirnames, filenames in os.walk(directory, topdown=True):
31 32
32 abspath = os.path.abspath(dirpath) 33 abspath = os.path.abspath(dirpath)
33 parent = os.path.dirname(abspath)) 34 basename = os.path.basename(abspath)
35 parent = os.path.dirname(abspath)
34 level = depth(abspath) - top 36 level = depth(abspath) - top
35 37
36 # sort articles of interest 38 # sort articles of interest
37 for resource in (dirnames, filenames): 39 for resource in (dirnames, filenames):
38 resource[:] = sorted(resource, key=sort_key) 40 resource[:] = sorted(resource, key=sort_key)
40 # set last for current dirpath 42 # set last for current dirpath
41 if dirnames: 43 if dirnames:
42 last[os.path.abspath(dirpath)] = dirnames[-1] 44 last[os.path.abspath(dirpath)] = dirnames[-1]
43 if last.get(parent) == os.path.basename(abspath): 45 if last.get(parent) == os.path.basename(abspath):
44 pass 46 pass
47
48 retval.append('%s%s'% (str(level) * level, basename))
49 level += 1
50 retval.extend([('%s%s' % (str(level) * level, filename))
51 for filename in filenames])
45 52
46 # retval.append('%s%s%s %s' % ('│' * (indent-1), 53 # retval.append('%s%s%s %s' % ('│' * (indent-1),
47 # ('├' if basename == basename else '└') if indent else '', 54 # ('├' if basename == basename else '└') if indent else '',
48 # basename)) 55 # basename))
49 # filenames = sorted(filenames, key=lambda x: x.lower()) 56 # filenames = sorted(filenames, key=lambda x: x.lower())