The Walk() Function In Python

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 
import os

# Name of the top-level directory
dirName = "./exampledir";

# Start walking
for containingDirectory, directories, files in os.walk(dirName):
        # Restrict walking only on the top
        if containingDirectory == dirName:
            print("List of subdirectories present:");
            print("%s Count: %s"%(directories,len(directories)));
            print("List of files present:");
            print("%s Count: %s"%(files,len(files)));
            break;

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 
# while listing the contents of a directory
import os

# Name of the top-level directory
dirName = "./testdir";

# Exclude temporary folders from listing
for parent, directories, files in os.walk(dirName):
    for item in directories:
        if "tmp" in item or "temp" in item:
            directories.remove(item);
    
    # Print directories
    for item in directories:
        print("%s/%s"%(parent, item));

    # Print files
    for item in files:
        print("%s/%s"%(parent, item));  

Input:

walk() example in python

Output:

./testdir/docs

./testdir/docs/reviewed

./testdir/docs/inv.doc

./testdir/docs/ord.doc

 


Copyright 2023 © pythontic.com