[Tutor] clarification on os.path.walk
Luke Paireepinart
rabidpoobear at gmail.com
Tue Jun 12 08:32:37 CEST 2007
Chandrashekar wrote:
> Hi,
>
> I have a program like this.
>
> import os,os.path
> import re
>
> def print_files(arg,dir,files):
> '''print 'arg', arg
> print 'dir', dir
> print 'files',files'''
> for file in files:
> path = os.path.join(dir,file)
> path = os.path.normcase(path)
> if re.search(r".*\.txt",path):
> print path
>
>
> os.path.walk('.',print_files,1)
>
> 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.
>>> import os
>>> help(os.path.walk)
Help on function walk in module ntpath:
walk(top, func, arg)
Directory tree walk with callback function.
For each directory in the directory tree rooted at top (including top
itself, but excluding '.' and '..'), call func(arg, dirname, fnames).
dirname is the name of the directory, and fnames a list of the names of
the files and subdirectories in dirname (excluding '.' and '..'). func
may modify the fnames list in-place (e.g. via del or slice assignment),
and walk will only recurse into the subdirectories whose names remain in
fnames; this can be used to implement a filter, or to impose a specific
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.
HTH,
-Luke
More information about the Tutor
mailing list