3rd argument to os.path.walk(): Why?
James T. Dennis
jadestar at idiom.com
Mon Dec 11 07:13:31 EST 2000
In the _Python_Essential_Reference_, by David Beazley
(New Riders Publishing, 2000) I notice that os.path.walk()
takes three arguments which he lists as:
path, visitfunc, arg
It notes that the function referenced in visitfunc is called
with (arg, dirname, namelist) for every directory that is traversed
by the os.path.walk.
I don't understand what the "args" is for.
I can see how to use the other arguments. Here's a couple of
trivial examples:
import os
def print_dirnames(a, dir, flist):
print dir
def list_directories(a, d, l):
for each_file in d:
print "%s/%s" % (d, each_file)
os.path.walk("/", print_dirnames, '')
os.path.walk("/", list_directories, 'x')
... these both work as I expected. Of course I'm simply
ignoring the 'args' argument in my functions. I guess I
could write my functions to behave differently based on
the contents of the args. (I can even imagine that as
useful, though I don't need it for anything right now).
At first I though that this "args" element was required
to be a string. However, I tried calling the os.path.walk()
with numeric and anonymous (literal) lists and saw no
differences in the output as a result of those. I guess
I could call it with a lambda function or a reference to a
function if I wanted to. I don't know Python well enough to
show an example, but I could see where that might be useful.
However, my question boils down to:
Why is this third argument required in os.path.walk()?
More information about the Python-list
mailing list