Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > problem with recursion

Reply
Thread Tools

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

 
Reply With Quote
 
 
 
 
wittempj@hotmail.com
Guest
Posts: n/a
 
      03-03-2005
take a look at os.walk, it is documented in the library documentation

 
Reply With Quote
 
 
 
 
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
 
Reply With Quote
 
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)
 
Reply With Quote
 
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>

 
Reply With Quote
 
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))


 
Reply With Quote
 
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/
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
va_arg... recursion: changing arguments and the using recursion jononanon@googlemail.com C Programming 8 04-26-2012 08:37 PM
Simple recursion problem. Need help. Anakin Java 14 04-13-2005 03:10 AM
Re: Recursion problem - Graph theory - Algorithm needed Allan W C++ 4 01-22-2004 02:42 PM
Recursion problem - Graph theory - Algorithm needed JimC C++ 3 01-17-2004 12:43 PM
Recursion-related problem kelvSYC Java 2 11-18-2003 03:32 PM



Advertisments