problem with recursion

Steve Holden steve at holdenweb.com
Thu Mar 3 17:34:00 EST 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/



More information about the Python-list mailing list