Home » Python » Python os.walk() Method

About Yatin Batra

Yatin Batra
Yatin has graduated in Electronics & Telecommunication. During his studies, he has been involved with a large number of projects ranging from programming and software engineering to telecommunications analysis. He works as a software developer in the information technology sector where he is mainly involved with projects based on Java and J2EE technologies platform.

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
(0 rating, 0 votes)
You need to be a registered member to rate this.
Start the discussion Views Tweet it!
Do you want to know how to develop your skillset to become a Java Rockstar?
Subscribe to our newsletter to start Rocking right now!
To get you started we give you our best selling eBooks for FREE!
1. JPA Mini Book
2. JVM Troubleshooting Guide
3. JUnit Tutorial for Unit Testing
4. Java Annotations Tutorial
5. Java Interview Questions
6. Spring Interview Questions
7. Android UI Design
and many more ....
I agree to the Terms and Privacy Policy
Subscribe
Notify of
guest

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

0 Comments
Inline Feedbacks
View all comments