Python

Python List files in directory Example

Hello in this tutorial, we will understand the different ways to list the files in a directory using the python programming.

1. Introduction

To list the files in a directory we will use the two approaches i.e.

  • Using os module – We will use the walk method which will generate the file names in a directory tree by walking the tree either top-down or bottom-up. Represented with the following syntax – os.walk(path_name, topdown=False)
  • Using glob module – This module is used to fetch the filenames or the pathnames matching a specified pattern. Follows the standard Unix path expansion rules and also support the wild card patterns to make the retrieval simpler and convenient

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 os and glob module

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

Using os and glob module

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# JCG assignment - Python list files in directory example
import glob
import os
# approach1
# 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):
        for filename in files:
            print(filename)
# approach2
# The glob module also makes it possible to get a list of files or folders in a directory
# **/*.* - will support the recursive globs (where *.* denotes all files with all extensions)
def glob_module():
    for file_name in glob.iglob('**/*.*', recursive=True):
        print(file_name)
def main():
    print('\n . . . . Using the os module to list the files . . . . \n')
    os_module()
    print('\n . . . . Using the glob module to list the files . . . . \n')
    glob_module()
if __name__ == '__main__':
    main()
print('\nDone')

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
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
. . . . 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-key.py
jcg-assignment-list-files-in-a-directoy.py
jcg-assignment-list.py
jcg-assignment-python-remove-character-from-string.py
jcg-assignment-set.py
jcg-assignment-zip.py
 . . . . Using the glob module to list the files . . . .
jcg-assignment-key.py
jcg-assignment-list-files-in-a-directoy.py
jcg-assignment-list.py
jcg-assignment-python-remove-character-from-string.py
jcg-assignment-set.py
jcg-assignment-zip.py
aws-boto3-assignments\dynamodb\create_items.py
aws-boto3-assignments\dynamodb\delete_all_items.py
aws-boto3-assignments\dynamodb\delete_item.py
aws-boto3-assignments\dynamodb\get_all_items.py
aws-boto3-assignments\dynamodb\get_item.py
aws-boto3-assignments\dynamodb\songs.json
aws-boto3-assignments\dynamodb\update_item.py
main-function-assignment\MainFunction1.py
main-function-assignment\MainFunction2.py
main-function-assignment\sum\AddValues.py
main-function-assignment\sum\Sum.py
Done

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 and glob modules in the python programming to list the files in the directory
  • Sample program to understand the os and glob modules

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

4. Download the Project

This was an example of listing files in a directory using the Python.

Download
You can download the full source code of this example here: Python list files in directory example

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