tags:

views:

174

answers:

2

I'm currently using the walk method in a uni assignment. It's all working fine, but I was hoping that someone could explain something to me.

in the example below, what is the a parameter used for on the myvisit method?

>>> from os.path import walk
>>> def myvisit(a, dir, files):
...   print dir,": %d files"%len(files)

>>> walk('/etc', myvisit, None)
/etc : 193 files
/etc/default : 12 files
/etc/cron.d : 6 files
/etc/rc.d : 6 files
/etc/rc.d/rc0.d : 18 files
/etc/rc.d/rc1.d : 27 files
/etc/rc.d/rc2.d : 42 files
/etc/rc.d/rc3.d : 17 files
/etc/rc.d/rcS.d : 13 files
+4  A: 

It's the argument you gave to walk, None in the example in your question

Krumelur
ah right, ok. So could `a` just be `None` as well?
Aaron Moodie
just tried it. No. :) Thanks Krumeler.
Aaron Moodie
The usual way in Python to specify that an argument or variable is not of interest is to use the underscore ('_') character as the argument name. This is only a convention, but some IDEs (PyDev, for example) honors this when checking for unused variables.
Krumelur
ah great, that makes things easier. Thanks.
Aaron Moodie
A: 

The first argument to your callback function is the last argument of the os.path.walk function. Its most obvious use is to allow you to keep state between the successive calls to the helper function (in your case, myvisit).

os.path.walk is a deprecated function. You really should use os.walk, which has no need for a callback function and helper arguments (like a in your example) are not needed.

for directory, dirnames, filenames in os.walk(some_path):
    # run here your code
ΤΖΩΤΖΙΟΥ