[Tutor] clarification on os.path.walk

Alan Gauld alan.gauld at btinternet.com
Tue Jun 12 09:24:46 CEST 2007


"Luke Paireepinart" <rabidpoobear at gmail.com> wrote
> Chandrashekar wrote:
>> I have a program like this.
>> def print_files(arg,dir,files):
>>     for file in files:
>>         path = os.path.join(dir,file)
>>         path = os.path.normcase(path)
>>         if re.search(r".*\.txt",path):
>>             print path

>> My doubt is that what does the argument stand for in this program?
>> os.path.walk('.',print_files,0)
>> os.path.walk('.',print_files,1)
>> os.path.walk('.',print_files,2)
>>
>> all seems to be rendering the same output. Can anyone clarify when
>> this argument is used.

> Help on function walk in module ntpath:
>
> walk(top, func, arg)
> .....
>    order of visiting.  No semantics are defined for, or required of, 
> arg,
>    beyond that arg is always passed to func.  It can be used, e.g., 
> to pass
>    a filename pattern, or a mutable object designed to accumulate
>    statistics.  Passing None for arg is common.

So, inapplying this to the OPs case, you pass the values of 0,1,2 in
but then never use them in your print_files function. You could have
used it to pass in the required extension like this:

def print_files(arg,dir,files):
     for file in files:
         path = os.path.join(dir,file)
         path = os.path.normcase(path)
         regex = r".*%s" % arg    # <--- Use arg to change the regex
         if re.search(regex, path):
             print path

And then called it with different values like:

os.path.walk('.',print_files,"\.txt")
os.path.walk('.',print_files,"\.py")
os.path.walk('.',print_files,"\.doc")

If you try that you should see a difference.

Personally I prefer using the slightly simpler os.walk() for
this kind of thing.

HTH,

-- 
Alan Gauld
Author of the Learn to Program web site
http://www.freenetpages.co.uk/hp/alan.gauld 




More information about the Tutor mailing list