Python

Python os.walk() Method

Hello in this tutorial, we will understand the os.walk method to list the files in a directory using the python programming.

1. Introduction

The os.walk(……) method will generate the file names in a directory tree by walking the tree either top-down or bottom-up. Represented with the following syntax –

1
os.walk(path_name, topdown=False, onerror=None, followlinks=False)

Where,

  • path_name denotes the directory path which is to be scanned
  • topdown is an optional argument. If the argument is set to true or unspecified the directories are scanned from top-down. If the argument is set to false the directories are scanned from bottom-top
  • onerror is an optional argument which shows an error to continue to walk or raise an exception to abort the walk
  • followlinks is an optional argument which is set to true visits the directories pointed by the symlinks

1.1 Setting up Python

If someone needs to go through the Python installation on Windows, please watch this link. You can download the Python from this link.

2. Python List files in directory Example

I am using JetBrains PyCharm as my preferred IDE. You are free to choose the IDE of their choice.

2.1 Using the os module

Let us understand this with the help of a code snippet.

Using the os module

01
02
03
04
05
06
07
08
09
10
11
12
13
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()

If everything goes well the following output will be shown in the IDE console.

Console Output

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
. . . . Using the os module to list the files . . . .
create_items.py
delete_all_items.py
delete_item.py
get_all_items.py
get_item.py
songs.json
update_item.py
AddValues.cpython-38.pyc
AddValues.py
Sum.py
MainFunction1.py
MainFunction2.py
jcg-assignment-default-constructor.py
jcg-assignment-key.py
jcg-assignment-list-files-in-a-directoy.py
jcg-assignment-list.py
jcg-assignment-map.py
jcg-assignment-os-walk.py
jcg-assignment-parameterized-constructor.py
jcg-assignment-python-remove-character-from-string.py
jcg-assignment-set.py
jcg-assignment-string-replace.py
jcg-assignment-zip.py

That is all for this tutorial and I hope the article served you with whatever you were looking for. Happy Learning and do not forget to share!

3. Summary

In this tutorial, we learned –

  • An introduction to os module in the python programming to list the files in the directory
  • Sample program to understand the os module

You can download the source code of this tutorial from the Downloads section.

4. Download the Project

This was an example to list the files in the directory using the python programming.

Download
You can download the full source code of this example here: Python os.walk() Method

Yatin Batra

An experience full-stack engineer well versed with Core Java, Spring/Springboot, MVC, Security, AOP, Frontend (Angular & React), and cloud technologies (such as AWS, GCP, Jenkins, Docker, K8).
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button