[Tutor] return values from function

Jorge Louis De Castro jorge at bcs.org.uk
Sat Jul 30 09:48:19 CEST 2005


Hi all,

I have the following code to traverse a directory structure from a given 
path:

import os, fnmatch, re
folders = []
r = re.compile(r'file_filter') # msn_username is passed by the user

def browse(junk, dirpath, namelist):
  for name in namelist:
    if r.search(name):
      folders.append(os.path.join(dirpath, name))

userbase = os.environ['USERPROFILE']
# change directory to user profile folder
os.chdir(userbase)
os.path.walk(os.getcwd(), browse, None)

Now with the sample above I need global variables ("folders") to keep track 
of the directories traversed. Ideally I'd like to either have the browse 
function return a value that I could use or I'd like the ability to pass on 
arguments to the browse function, which I can't seem to be able to.

Any help would be appreciated.

Chrs

j. 


More information about the Tutor mailing list