problem with recursion

V

vegetax

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
###
 
K

Kent Johnson

vegetax said:
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
 
A

Alexander Zatvornitskiy

ðÒÉ×ÅÔ 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 address removed)
 
V

vegetax

Alexander said:
?????? 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 address removed)

Thanks it works!

Kent said:
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>
 
V

vegetax

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

Steve Holden

vegetax said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top