[Tutor] Pythonic? Building a full path from a "visual" file tree

stv stvsmth at gmail.com
Wed Mar 22 00:44:10 CET 2006


On 3/21/06, stv <stvsmth at gmail.com> wrote:
>
> import string
>
> def expand_tree(filetree):
>   indent = '\t'
>   stack = []
>   for f in filetree:
>     indents = f.count(indent)
>     while len(stack) > indents: stack.pop()
>     stack.append(f.strip())
>     yield string.join(stack,'')
>     if not f.endswith('/'): stack.pop()

> The real-world solution will use a regex to find run
> of spaces at the start of element 'f'.

Hmmm, I'm having to rethink this ... regex will probably not do what I
want (count instance of "indent" ... or, even if regex will do,
there's probably a cleaner solution without it.

Given the subversion problem space (where I can count on 1 space for
each level of child-ness), are there cleaner solutions than this:

indents = len(f) - len(f.lstrip())


More information about the Tutor mailing list