os.path.walk(path, visit, arg)
Traverses through a path and all its child directories recursively, and runs a function for each of the directories encountered.
Important: Symbolic links are not treated as directories, and walk will not follow them! You will need to identify and process them manually (see the example below).
Important: In Python 3, this function was removed in favor of os.walk().

Arguments

path
The base path. This is the starting directory to walk through. This path will be included in the results.
visit
A callback function to run for every (sub-)directory that is encountered. The function is called with the arguments (arg, dirname, names).
arg
The arg argument that is specified in the original call to os.path.walk. Useful for passing on arbitrary information that is necessary for processing the found (sub-)directories.
dirname
The path of the directory that was encountered. This is a full (absolute) path.
names
The names of all the items that were encountered in the directory. This includes sub-directories.
arg
An arbitrary extra argument that will be passed on to the specified callback function.

Return values

Always returns None.

Examples

Example: Using os.path.walk
Code:
def process_dir(arg, dirname, names):
	print "[%s] Found at %s: %s" % (arg, dirname, ", ".join(names))

os.path.walk("/home/sven/sample", process_dir, "Test")
Output:
[Test] Found at /home/sven/sample: testfile1.txt, testdir1, testdir2
[Test] Found at /home/sven/sample/testdir1: testfile4.txt, testfile3.txt, testdir1a
[Test] Found at /home/sven/sample/testdir1/testdir1a: testfile2.txt
[Test] Found at /home/sven/sample/testdir2: testfile5.txt
Example: Dealing with symlinks in os.path.walk
Code:
def process_dir(arg, dirname, names):
	for name in names:
		name_path = "%s/%s" % (dirname, name)
		if os.path.islink(name_path):
			os.path.walk(name_path, process_dir, arg)
	print "[%s] Found at %s: %s" % (arg, dirname, ", ".join(names))

os.path.walk("/home/sven/sample", process_dir, "Test")
Output:
[Test] Found at /home/sven/sample/testdir3: testfile5.txt
[Test] Found at /home/sven/sample: testfile1.txt, testdir3, testdir1, testdir2
[Test] Found at /home/sven/sample/testdir1: testfile4.txt, testfile3.txt, testdir1a
[Test] Found at /home/sven/sample/testdir1/testdir1a: testfile2.txt
[Test] Found at /home/sven/sample/testdir2: testfile5.txt