Python

Python Os.walk Example

Walk function in any operating system is like the os.path. The walk function generates the file names in a directory tree by navigating the tree in both directions, either a top-down or a bottom-up transverse. Every directory in any tree of a system has a base directory at its back. And then it acts as a subdirectory. The walk () generates the output in three tuples, the path, directory, and the files that come in any subdirectory.

  • Dirpath: It is a string that leads the files or a folder towards the path to the directory.
  • Dirnames: All the subdirectories names that don’t contain ‘.’ And ‘..’.
  • Filenames: The directory path that contains files other than directory files. It is a list of folders or files that may be system-created or user-created files.

The names present in the list do not contain any component of the path. To fetch the full path that starts from the top to a directory or file in the directory path, we use os.walk.join () that has arguments of dirpath and the directory name.

Top-down and bottom-up are the two optional arguments at a time. This means that either one of the options is to be used in the function if we want a sequence of directories. Whereas in some cases, the top-down sequence is selected by default if we do not mention any argument regarding this sequence. If the argument top-down is True, the triple for the main directory is displayed first and then the subdirectories later on. And if the top-down is false, the triple for the directory is displayed after that for the subdirectories. In other words, the sequence is in a bottom-up manner.

When the top-down situation is True, the user can update the directory name list, and then walk() will only be applied on the subdirectories. Whereas updating the names of directories when the top-down is false is inapplicable. This is because, in the bottom-up mode, the directories names in the directories are displayed before the directory path. Listdir() function can eliminate the errors by default.

Python Os.Walk () working

Let’s see how the file system is traversed in Python.  It works like a tree having a single root that further divides into branches. And the branches are expanded as sub-branches and so on. This walk function outputs the names of files in a directory tree by navigating the tree either from the top or from the bottom.

Syntax of Os.walk()

# os.walk(top[, topdown=True[ onerror=None[ followlinks=False]]])

top = It is the head or a starting point of any subdirectory traverse. It yields 3 tuples, as we have described at the start of the article.

Topdown = In the case when it is true, the scanning of directories is from top to the end and vice versa in the opposite case.

Oneroor = This is a special feature that is designed to monitor the error. It can either show an error to keep going with the walk or raise the exception to dismiss the walk.

Follow links = Leads to unstoppable recursions; it is set to true.

Note: The followlinks option is set as true; it leads to unstoppable recursions if any link points to the base directory of its own. The walk () function does not take the record of the directories that it has already traversed.

Example 1

All the files in the directory are to be listed by using this function. Consider a snippet of code. The first step is to import the OS module, like other features to be imported by the python library.

# Import os

After that, we will define a function named ‘os module’. Inside this function, use a for loop to get all the files following the directories and the root.  The top-down technique is used here. And “followlinks” is kept True.

import os
# The os module provides a function that gets a list of files or folders
# in a directory
# '.' signifies the current folder
# walk(....) method generates the file names in a directory tree by walking the
# tree either top-down or bottom-up
def os_module():

    for root, dirs, files in os.walk('.', topdown=False, onerror=None, followlinks=True):
        for filename in files:
            print(filename)


def main():
    print('\n ... Using the os module to list the files . . . \n')
    os_module()


if __name__ == '__main__':
    main()

This module will only print the filenames in the directory. The ‘.’ full stop we used here is specifically for the current folder. In the main program, we will call the function declared.

In the output, you can see the filenames in the current folder.

Example 2

This function will return the value by scanning all the directories and the subdirectories in the current path of the directory from the bottom to up direction, as top-down = False here.

Os.walk(“.”, topdown = False)
#!/usr/bin/python
import os
for root, dirs, files in os.walk(".", topdown=False):
    for name in files:
        print(os.path.join(root, name))
    for name in dirs:
        print(os.path.join(root, name))

A for loop is used to print the files and directories separately. The “os.path.join” brings the name and the main directory from the path.

A small part of the output is shown above. If one wants to get the output in the top to down order, then the module of top-down should be kept as True.

# Os.walk(“.”, topdown = True)

Example 3

This example differs from the previous ones in the type of parameters used. Here the function takes only the ‘path’. A for loop is used to display the values of files, directories of the path. If-statement is used to limit the resultant value in every line at a time. Here we have used 4. After every 4 words, the value will be shifted towards the next line. The starting value of the ‘I’ variable is taken as zero.

import os
path = "C:\\Users"
i = 0
for (path, dirs, files) in os.walk(path):
    print(path)
    print(dirs)
    print(files)
    print("----")
    i += 1
    if i >= 4:
        break

The respective output is shown below. The path, directories, and files are displayed as output.

Example 4

Like the walk () method in OS, we can also use the “os.listdir()” alternative to the path, which will display all the values of the particular value. i.e., here, we have used files to be printed. The path is taken as an argument for the function. The resultant value will be stored in the files variable. For loop will display all the files in the respective directory.

# Files = os.listdir(path)
import os
path = '/Users/USER/.spyder-py3'
files = os.listdir(path)
for f in files:
print(f)

The list is displayed here that contains all the files in the respective directory.

Example 5

You have seen the examples in which all the folders or paths are displayed, even those we want to hide; they are also exposed. But “os.walk()” uses some features that allow excluding the hidden directories.

After importing the OS module, we have introduced the path which we will use in the example.

# Dirs.[:] = [d for d in dirs. If not d.startswith(‘.’)]
import os
path = 'C:/Users/USER/.spyder-py3'
for root, dirs, files in os.walk(path):
    print(root)
dirs[:] = [d for d in dirs if not d.startswith('-')]
for dir in dirs:
    print(os.path.join(root, dir))
for file in files:
    print(os.path.join(root, file))

This feature is capable of hiding the directories, now with this list, the hidden directories are not included in the output.

Example 6

Suppose you have a list of names of the directory that you want to neglect during the walk () function. One way is to use the method as described above. The second way is going to be explained here. This will give the same result.

# Dirs.[:] = []
import os
path = 'C:/Users/USER/.spyder-py3'
for root, dirs, files in os.walk(path):
    if root in ignore_list:
        dirs[:] = []
        files[:] = []
        print(dirs[:])

Example 7

If you want to take the print of the absolute values, the names of subdirectories, and the directories but not the whole path, in this case, the python walk function is used.

import os
x = r'C:/Users/USER/.spyder-py3'
for r, d, f in os.walk(x):
    print(d)

From the output, you can see that the resultant values have become limited.

Conclusion

The ‘Python os walk’ function is used to traverse all the paths in a directory, from top to bottom or from the bottom to the top. We have also seen how to hide the unwanted data to be previewed. Surely this article will be a helping edge for implementing the walk function of the OS module in Python.

About the author

Aqsa Yasin

I am a self-motivated information technology professional with a passion for writing. I am a technical writer and love to write for all Linux flavors and Windows.