Alexander Zatvornitskiy wrote: > ?????? 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, [EMAIL PROTECTED]
Thanks it works! Kent Johnson wrote: >The problem is that you are always appending individual names to the same >list. If you make rec() >return a list and append that to the current list you get what you want: >#!/usr/bin/python >from os import listdir >from os.path import isdir,join,basename >dirpath = '/tmp/test/' >def rec(f): > res = [] > for ele in listdir(f): > res.append(ele) > ele = join(f,ele) > if isdir(ele): > res.append(rec(ele)) > return res > >print rec(dirpath) Thanks that works too! I realy have to improve my recursion skills =( Anyway having the directory : +root +doc1 lesson1 lesson2 +doc3 lesson3 +doc2 lesson4 lesson5 With the code : <code> #!/usr/bin/python from os import listdir from os.path import isdir,join,basename import HTMLgen dirpath = '/devel/python/html/test' def rec(f): res = [] for ele in listdir(f): res.append(ele) ele = join(f,ele) if isdir(ele): res.append(rec(ele)) return res print HTMLgen.List(rec(dirpath)) </code> Gives me the espected output as a HTML representation of the tree: <UL> <LI>doc1 <UL> <LI>lesson1.html <LI>lesson2.html <LI>doc3 <UL> <LI>lesson3.html </UL> </UL> <LI>doc2 <UL> <LI>lesson5.html <LI>lesson4.html </UL> <LI>.tmp3.py.swp </UL> -- http://mail.python.org/mailman/listinfo/python-list