problem with recursion

Alexander Alexander_Zatvornitskiy at p131.f3.n5025.z2.fidonet.org
Thu Mar 3 14:30:34 EST 2005


Привет vegetax!

03 марта 2005 в 13:54, vegetax в своем письме к All писал:

 v> I need this in order to print a directory tree with htmlgen library
 v> which uses nested lists to represent trees.
As you see from output of your code, you simply add items to the only list. Try
this:
 v> def rec(f):
        res=[]
 v>     print f
 v>     for ele in listdir(f):
 v>       ele=join(f,ele)
 v>       if isdir(ele):
 v>           # append the directory name
 v>           res.append(basename(ele))
              res+=[ rec(ele) ]
 v>       else:
 v>     res.append(basename(ele))
        return res

print rec(dirpath)



Alexander, zatv at bk.ru



More information about the Python-list mailing list