How Do I List All Files of a Directory in Python?

Problem: Given a directory. How to list all the files in the directory using Python?

Video Solution

 What is a directory?
A directory is a unit organizational structure used to store a collection of programs and subdirectories.

Sometimes as a coder you have to deal with the countless number files within different directories. Hence, it becomes reasonable to organize these files within directories. In this article, we are going to look at the various methods on how to list all the files of a directory?

Recommended Article: Python Delete File [Ultimate Guide]

โœจMethod 1: Using The os Module

The OS module is a module in Python that has various pre-defined functions which can be used to work upon the directories. You can use the OS module for performing the following operations on directories:

  1. To create and remove a directory.
  2. Listing the files of a directory.
  3. Changing the current directory.

To use the os module and its functions, you have to first import it into your code as shown below:

import os

Once you have successfully imported the os module, you can use the following functions to work with directories:

os.listdir()

The os.listdir() method in Python is used to list all the files and directories present inside a specified directory. If you do not specify any directory then he list of all files and directories in the current working directory is returned.

Syntax: os.listdir(path)

Parameters:
path (optional) : path of the directory.

Return Type: list

Example:

# Importing the os module 
import os
  
# Listing all the files in directories
path = "/"
lst = os.listdir(path)
# Printing the list of files  
print("All the files in directory: '", path, "' :") 
print(lst)

Output:

All the files in directory: ' / ' :
['sys', 'boot', 'mnt', 'dev', 'run', 'tmp', 'proc', 'var', 'srv', 'home', 'etc', 'bin', 'lib64', 'usr', 'lib', 'opt', 'sbin', 'media']

Note: You can use a for loop to list all the files in the directory individually.

You can use the .endswith() method to return the specific files that you want from the directory. It filters the files based on the extensions provided. In the following example, we will only return the “text files” from the current directory:

Example:

# Importing the os module 
import os
  
# Listing all the files in directories
path = "."
lst = os.listdir(path)
# printing only the text files from the directory
for file in lst:
    if file.endswith(".txt"):
        print(file)

Output:

demo.txt
demo2.txt
demo3.txt

os.walk()

os.walk() is a method in Python that is used to return a list of three things:

  1. The name of the root directory.
  2. A list of the names of the sub-directories.
  3. A list of the file names in the current directory
Syntax: os.walk(path)

Example:

# Importing the os module
import os

# Printing all the files in directories using os.walk()
for root, dirs, files in os.walk("/"):
    for file in files:
        print(file)

Output:

Sys
Boot
Mnt
Proc 
var 
srv 
home
etc
bin
lib
media
opt

os.scandir()

The os.scandir() method got introduced in Python 3.5 and is one of the latest methods in Python used to list all the files in a directory. This method returns an iterator instead of the list.

Syntax: os.scandir(path)

Example:

# Importing the os module 
import os
path = "/"
lst = os.scandir(path)
  
# Printing all the files from the directory
print("All the files in the directory:")
for files in lst:
    if files.is_dir() or files.is_file():
        print(files.name)

# As os.scandir() returns an iterator, it's necessary to close it.
lst.close()

Note: The files.is_file() method will check if item is a file or not and the files.is_dir() will check if item is a directory.

Output:

Sys
Boot
Mnt
Proc 
var 
srv 
home
etc
bin
lib
media
opt

โœจMethod 2: Using The glob Module:

Generally, the developers have a specific requirement when they want to list the files of a directory. The glob module in Python has a few built-in functions that can help in listing files of a specified directory.

To use the glob module and its functions, you have to first import it into your program as shown below. It is a built-in module in Python. You do not have to install it externally, however, it is necessary to import it.

import glob

glob.glob()

The glob.glob() method in Python provides wildcards like “*”, “?”, [ranges]  that make the process of retrieving a path easy. The “*” character helps to match the items in the current directory.

Syntax: glob.glob(path, wildcard)

Example:

# Importing the glob module
import glob

# Listing all the files in directories using '*' pattern
print("All Files:")
for file in glob.glob('*'):
    print(file)

# search .txt files
print("Text files in the directory:")
for file in glob.glob('*.txt'):
    print(file)

Output:

All Files:
abc.txt
created nested directory.py
demo.txt
dir_A
list_files.py
work.txt
Text files in the directory:
abc.txt
demo.txt
work.txt

glob.iglob()

The glob.iglob() method can be used to print the filenames recursively. To do this, you have to set the recursive parameter to be True. The ** wildcard character alond with the recursive argument is used to search folders recursively.

Syntax: glob.iglob(path, wildcard, recursive = True)

Example:

# Importing the glob module 
import glob
path = "**/*"
  
# Listing all the files from the directory
lst = glob.iglob(path, recursive=True)
print("All the files in the directory:")
for file in lst:
    print(file)

Output:

Sys
Boot
Mnt
Proc 
var 
srv 
home
etc
bin
lib
media
opt

Impotant Info: The code given below will search for all the text files in the current directory or folder.

# Importing the glob module 
import glob
path = "./**/*.txt"
  
# Listing all the files from the directory
lst = glob.iglob(path, recursive=True)
# printing the text files from the directory
for file in lst:
    print(file)

Output:

demo.txt
demo2.txt
demo3.txt

Conclusion

In this article, we studied various types of methods (Using the os and glob modules) to list all the files of a directory in Python. I hope this tutorial helped you. Please stay tuned and subscribe for more solutions and interesting discussions in the future. Happy coding!

๐Ÿ‘‰ Recommended Tutorial: How to Find All Files on a Computer That Match a Given Regex Pattern

โœ๏ธPost Credits: SHUBHAM SAYON and RASHI AGARWAL


Python Regex Course

Google engineers are regular expression masters. The Google search engine is a massive text-processing engine that extracts value from trillions of webpages.ย ย 

Facebook engineers are regular expression masters. Social networks like Facebook, WhatsApp, and Instagram connect humans via text messages.ย 

Amazon engineers are regular expression masters. Ecommerce giants ship products based on textual product descriptions.ย ย Regular expressions โ€‹rule the game โ€‹when text processing โ€‹meets computer science.ย 

If you want to become a regular expression master too, check out the most comprehensive Python regex course on the planet: