Introduction to the Python os.walk function with a practical example

Being rich in features, the Python computer programming language offers to the coder many utilities which they can make use of to automate almost any kind of task in their operating system. Although the Python technology offers all kinds of modules in its standard library, I never truly liked its official documentation. Not that I hate it, but I don’t find it very user friendly, especially for the complete novice coder.

As far as my experience goes with the Python’s official documentation, I can assure you that there are not many concrete examples in there.

Having some good knowledge on the Python’s os module, I decided to share a practical example which explains the usage of the walk() function being found in this standard library.

What's the one thing every developer wants? More screens! Enhance your coding experience with an external monitor to increase screen real estate.

First of all, make sure to launch a new interactive console in your own operating system so you can practice everything being shared in this article by yourself.

Once you have managed to do that, continue with the rest of this tutorial.

Introduction to the os.walk function

According to the Python’s official documentation about the os builtin module, the os.walk() function is used to generate the file names in a directory tree by walking the tree either top-down or bottom-up. As the official documentation reads, the os.walk builtin function generates a 3-tuple for each one of the directories in the tree, including the root itself.

In other words, the os.walk builtin function returns all the directory names and filenames inside a root tree.

Practice os.walk yourself

Before one can make use of the Python’s builtin functionalities included in the standard library, they need to include them in their session first through the Python’s import statement.

Let’s import the Python’s builtin os module by making use of the following piece of code.

import os
Once you have managed to import the os module like shown above, run the following Python’s command to access the walk() function.

walk_function = os.walk()

Calling the os.walk() without an argument, like shown in the above piece of Python code, will result in the following error.

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: walk() takes at least 1 argument (0 given)

As you can see from the above traceback produced in the interactive Python console, the os.walk() function takes at least one argument. Since we called it without an argument, we got an error.

From my personal experience with this Python function; we need to run it with a root directory as an argument.

So let’s define a root directory as a Python string object like shown below.

root_dir = '/Users/oltjano/Desktop/Test'

Make sure to define the root_dir according to the configurations in your local operating system.

Now let’s run the os.walk function with the root_dir as the first argument.

os.walk(root_dir)

Once the above piece of Python code got executed in my Python interactive console, I got the following output.

<generator object walk at 0x10d1e5a50>

As you can see from the above output, a call to the os.walk function produces a generator object. For those of you who don’t know, the Python generator object returns multiple results to the user one by one.

We can use a for loop to access the results of the generator object.

gen_obj = os.walk(root_dir)
for root_dir, dir_names, file_names in gen_obj:
    print(root_dir, dir_names, file_names)

After I executed the above piece of Python code in my own interactive console, I got the following output.

('/Users/oltjano/Desktop/Test', ['test1', 'test2'], ['.DS_Store', 'test.py', 'test.txt'])
('/Users/oltjano/Desktop/Test/test1', [], [])
('/Users/oltjano/Desktop/Test/test2', [], [])

Based on the configuration of your root_dir you are going to get a different output, but the format is the same. As you can see from the output generated above, the os.walk does the entire tree of a root directory.

Let’s see another example, similar to the one shown above.

Another practical example with os.walk

First, let’s build a custom root directory from scratch in our machine. It’s tree should look like the one being shown below.

root_directory
    sub_root1
        test.txt
    sub_root2
        test1.txt
        test2.txt

Once you have managed to do that, then create a variable with the name of the root directory like shown below.

custom_root_dir = '/Users/oltjano/Desktop/root_directory'

Then call the os.walk function like shown below.

new_gen_obj = os.walk(custom_root_dir)

Then make use a for loop statement to access the results of the generator object being produced.

for root_dir, dir_names, file_names in new_gen_obj:
    print(root_dir, dir_names, file_names)

The result of the above piece of Python code should match the tree of the custom directory which we created.

Final thoughts

The Python’s os builtin module provides various functionalities which can be utilized to interact with many kinds of operating systems such as Unix, Linux and Windows. As I mentioned earlier, the Python’s official documentation lacks concrete examples, so I decided to share my experience on the os.walk function through this article.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download

Leave a Reply

Your email address will not be published. Required fields are marked *

*