Connecting Tech Pros Worldwide Forums | Help | Site Map

problem with recursion

vegetax
Guest
 
Posts: n/a
#1: Jul 18 '05
I am trying to make convert a directory "tree" in a list of list, but cant
find the algoritm =( ,for example a directory tree like :

+root
*+doc
***ele1
***ele2
***+doc3
****ele3
*+doc2
**ele4

*ele5

should convert into:

root[
*****doc,
*****[ele1,ele2,doc3,[ele3]],
*****doc2,
*****[ele4],
*****ele5
****]

I dont have any idea how to do it =( ,so far i get something like
this : [doc1,ele1,ele2,doc3,ele3,doc2,ele4,ele5] instead of
*******[doc1,[ele1,ele2,doc3,[ele3]],doc2,[ele4],ele5]

I need this in order to print a directory tree with htmlgen library which
uses nested lists to represent trees.

#!/usr/bin/python
from os import listdir
from os.path import isdir,join,basename

dirpath**=*'/tmp/test/'
res = []

def rec(f):
****print*f
****for*ele*in*listdir(f):
********ele*=*join(f,ele)
********if*isdir(ele):
# append the directory name
res.append(basename(ele))
************rec(ele)
********else*:
************res.append(basename(ele))

rec(dirpath)
print res
###


wittempj@hotmail.com
Guest
 
Posts: n/a
#2: Jul 18 '05

re: problem with recursion


take a look at os.walk, it is documented in the library documentation

Kent Johnson
Guest
 
Posts: n/a
#3: Jul 18 '05

re: problem with recursion


vegetax wrote:[color=blue]
> I am trying to make convert a directory "tree" in a list of list, but cant
> find the algoritm =( ,for example a directory tree like :
>
> #!/usr/bin/python
> from os import listdir
> from os.path import isdir,join,basename
>
> dirpath = '/tmp/test/'
> res = []
>
> def rec(f):
> print f
> for ele in listdir(f):
> ele = join(f,ele)
> if isdir(ele):
> # append the directory name
> res.append(basename(ele))
> rec(ele)
> else :
> res.append(basename(ele))
>
> rec(dirpath)
> print res[/color]

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)

Kent
Alexander Zatvornitskiy
Guest
 
Posts: n/a
#4: Jul 18 '05

re: problem with recursion


Привет 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@bk.ru
vegetax
Guest
 
Posts: n/a
#5: Jul 18 '05

re: problem with recursion


Alexander Zatvornitskiy wrote:
[color=blue]
> ?????? 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@bk.ru[/color]

Thanks it works!

Kent Johnson wrote:
[color=blue]
>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:[/color]
[color=blue]
>#!/usr/bin/python
>from os import listdir
>from os.path import isdir,join,basename[/color]
[color=blue]
>dirpath**=*'/tmp/test/'[/color]
[color=blue]
>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)[/color]

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>

vegetax
Guest
 
Posts: n/a
#6: Jul 18 '05

re: problem with recursion



How can i use a counter inside the recursive function?
This code gives me the error 'local variable 'c' referenced before
assignment'

#!/usr/bin/python
from os import listdir
from os.path import isdir,join,basename
import HTMLgen

dirpath = '/devel/python/html/test'
COUNTER = 0

def rec(f):
res = []
COUNTER += 1
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))


Steve Holden
Guest
 
Posts: n/a
#7: Jul 18 '05

re: problem with recursion


vegetax wrote:[color=blue]
> How can i use a counter inside the recursive function?
> This code gives me the error 'local variable 'c' referenced before
> assignment'
>[/color]
I really don't think it does. I'd believe "local variable 'COUNTER'
referenced before assignment".

Rule one: always copy and paste the traceback ...

I presume you want a count of the directories you have traversed?
[color=blue]
> #!/usr/bin/python
> from os import listdir
> from os.path import isdir,join,basename
> import HTMLgen
>
> dirpath = '/devel/python/html/test'
> COUNTER = 0
>
> def rec(f):[/color]

global COUNTER
[color=blue]
> res = []
> COUNTER += 1
> 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))
>
>[/color]

regards
Steve
--
Meet the Python developers and your c.l.py favorites March 23-25
Come to PyCon DC 2005 http://www.pycon.org/
Steve Holden http://www.holdenweb.com/
Closed Thread


Similar Python bytes