[Tutor] Pythonic? Building a full path from a "visual" file tree
stvsmth at gmail.com
Tue Mar 21 18:42:25 CET 2006
I would like to expand the "visual" representation of a tree
hierarchy, given below, where child-ness is defined by indentation,
and folder-ness is highlighted with a trailing '/'
The desired output is a fully qualified path for each input line:
I considered several brute-force solutions, but I persevered and came
to, what I think, is a more Pythonic solution. What do you think?
indent = '\t'
stack = 
for f in filetree:
indents = f.count(indent)
while len(stack) > indents: stack.pop()
if not f.endswith('/'): stack.pop()
lines = [line.rstrip() for line in file('test.txt')]
for i in expand_tree(lines): print i
Those familiar with subversion (particularly, svnlook tree
/path/to/repos) may recognize the problem space and the sample input
(except that I simplified things a bit here, using a tab to indicate
level-of-hierarchy instead of a single space, which could appear in
the filename). The real-world solution will use a regex to find run
of spaces at the start of element 'f'. It will also get input from
stdin instead of a file.
Is the list comprehension the right idiom for getting a file into a
list, without the EOL chars? I'm hard-pressed to see how it could be
more concise, but this is Python :) and the rtrim() feels a little
Is string.join() preferred to ''.join? Does importing all of string
outweigh the readability of string.join()?
Any subversion geeks know of way to eliminate this code with a native
Did other folks choke on generators for as long as I did? I'm not
going to admit, publicly, how long that was :) But I found this little
g = gen()
>>> [stack trace]
Beyond simple, but the obviousness of it is hidden in many of the
examples I saw.
More information about the Tutor