problem with recursion

Discussion in 'Python' started by vegetax, Mar 3, 2005.

  1. vegetax

    vegetax Guest

    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
    ###
     
    vegetax, Mar 3, 2005
    #1
    1. Advertising

  2. vegetax

    Guest

    take a look at os.walk, it is documented in the library documentation
     
    , Mar 3, 2005
    #2
    1. Advertising

  3. vegetax

    Kent Johnson Guest

    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
     
    Kent Johnson, Mar 3, 2005
    #3
  4. ðÒÉ×ÅÔ 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,
     
    Alexander Zatvornitskiy, Mar 3, 2005
    #4
  5. vegetax

    vegetax Guest

    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,


    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, Mar 3, 2005
    #5
  6. vegetax

    vegetax Guest

    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))
     
    vegetax, Mar 3, 2005
    #6
  7. vegetax

    Steve Holden Guest

    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/
     
    Steve Holden, Mar 3, 2005
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. kelvSYC

    Recursion-related problem

    kelvSYC, Nov 18, 2003, in forum: Java
    Replies:
    2
    Views:
    331
    Eric Sosman
    Nov 18, 2003
  2. Anakin
    Replies:
    14
    Views:
    760
    Thomas G. Marshall
    Apr 13, 2005
  3. JimC
    Replies:
    3
    Views:
    533
  4. Allan W
    Replies:
    4
    Views:
    544
    Jos A. Horsmeier
    Jan 22, 2004
  5. Replies:
    8
    Views:
    785
    John Reye
    Apr 26, 2012
Loading...

Share This Page