Function Signature:
walk(top, topdown=True, onerror=None, followlinks=False)
Parameters:
top - Name of the directory whose contents to be walked.
topdown - Denotes the walking direction. The deafult value is True, meaning the walk will start from the top of the directory. If False the walk will start from the bottom of the directory.
For a directory the walk() function returns a triple of <parent-name, dir-list, file-list>. In topdown approach exclusion of a specific subdirectory from the dir-list is possible. In bottom-up approach this exclusion is not possible as the triple for the bottom most directory is produced first.
onerror - The function to be called when an exception occurs during the walk. This function is of the signature onWalkError(exception). An object of type OSError will be passed to this function by Python in case of an error during the walk.
followlinks - A Boolean value that specifies whether a link in unix-like operating systems or a shortcut to a folder in windows to be included in the walk or not. One has to be careful about the circular links connecting back to the same parent directory while using the followlinks parameter.
Return Value:
A generator
Overview:
- The walk() function provides the names of files and directories, present in a given directory.
- Subdirectories are walked recursively.
- As the function walks through a given directory, it provides the following information:
- Name of the directory
- List of subdirectories underneath
- List of files underneath
Example 1:
# Example Python program to list the files and directories of a given directory # Name of the top-level directory # Start walking |
Output:
List of subdirectories present: ['childdir1', 'childdir'] Count: 2 List of files present: ['test.txt'] Count: 1 |
Example 2:
# Example Python program that excludes folders tmp, temp
|
Input:
Output:
./testdir/docs ./testdir/docs/reviewed ./testdir/docs/inv.doc ./testdir/docs/ord.doc |