Velocity Reviews > problem with recursion

# problem with recursion

vegetax
Guest
Posts: n/a

 03-03-2005
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
*******[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

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

Kent Johnson
Guest
Posts: n/a

 03-03-2005
vegetax wrote:
> 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

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

 03-03-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, http://www.velocityreviews.com/forums/(E-Mail Removed)

vegetax
Guest
Posts: n/a

 03-03-2005
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, (E-Mail Removed)

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>

vegetax
Guest
Posts: n/a

 03-03-2005

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

 03-03-2005
vegetax wrote:
> How can i use a counter inside the recursive function?
> This code gives me the error 'local variable 'c' referenced before
> assignment'
>

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?

> #!/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):

global COUNTER

> 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))
>
>

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/