[Tutor] return values from function
Alan G
alan.gauld at freenet.co.uk
Sat Jul 30 14:00:43 CEST 2005
> I have the following code to traverse a directory structure from a
> given path:
I'm not sure what you are using the browse function for.
Normally with path.walk you would use browse)() to modify
the namelist argument value and thus modify the path walked
by path.walk(). You appear to be trying to build a list of
files matching a pattern?
Have you looked at the glob module? Is the name pattern too
complex to be expressed using glob? or the fnmatch filter()
function? (I notice you import fnmatch but don't appear to
use it?)
Otherwise I think modifying the global list is the best
you can do.
> 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)
HTH,
Alan G
Author of the Learn to Program web tutor
http://www.freenetpages.co.uk/hp/alan.gauld
More information about the Tutor
mailing list