recursion problem

Richard Gordon richard at
Sun Dec 10 21:45:02 EST 2000


I'm stuck on something that I thought would be easy, but has got me stumped.

The scenario is that I am using python to automate the construction 
of a web index page that uses one of the assorted 
expanding/collapsing file tree javascripts (e.g., it is organized 
into folders and files within folders based on the physical location 
of stuff on the server). This particular one uses <ul> and <li> tags 
along with some trick style stuff to expand or collapse various 
levels of the hierarchy and looks more or less like the windoze 
explorer stuff once the dust settles.

Anyway, walking the tree, figuring out what's a file and what's a 
directory and that kind of thing are easy, but where I'm getting 
stuck is in dealing with deeply nested folders. This will probably 
get screwed up in email, but what I want is something like:


So I am basically just reading the top level directory, then testing 
each item to see if it is a file or a folder. If it's a file, I just 
call a function that prints '<li><a href="' + fullpath + '">' + 
filename + '</a>' while if it's a directory, I go into another 
function that is supposed to do the initial <ul> stuff, then calls 
the file printer function for each file, then closes things out with 
</ul> before passing control back to the initial treewalker.

Anyway, this is getting all screwed up as far as the closing </ul> 
tags are concerned and I have managed to create the world's widest 
web page (kind of cool if you like fractals), hit a recursion limit, 
and otherwise toyed with it until I am exasperated. The question is 
how to drill down thru an arbitrary number of subdirectories and get 
the closing </ul> tags in  the right places since the nesting 
prevents just writing one out every time you exit a directory?

Thanks for any help you may have to offer.

Richard Gordon
Gordon Design
Web Design/Database Development

More information about the Python-list mailing list