Register Login

List all Files in a Directory using Python

Updated May 15, 2023

Several techniques can help users to fetch the list of files from a folder using Python methods and modules.

This article will discuss the four different Python methods used for retrieving the list of all the files, namely os.listdir('dir_path'), os.scandir('path'), os.walk('dir_path'), and Path.rglob('dir_path').

These methods have different applications in different scenarios, and the article highlights all the examples with explanations of these Python methods.

What is a directory?

A directory is a unique type of folder, a unit organizational setup in a computer's file system that includes only the details needed to access files or directories. These separate all the files and directories in a systematized format, which users can retrieve anytime according to the path and file names.

But before jumping to how to list all the files of a directory, users must ensure that their directories must have any of these files with .txt, .jpg, .png, .pdf, etc. Also, make sure that you have a Python IDE or any other compiler to run your program and test if these methods can list all the files of the directory.

Now let us see how all the above methods and the two modules, Os Module, and glob module, works in Python.

The Os Module:

Method 1: Using the os.listdir() of Os Module

The Python os.listdir() method helps users to display the list of all the entries' names in the directory provided by the path. By default, the method will display the current directory.

Beyond the first level of folders, it does not return the special entries '.' and '..' even if they exist in the directory.

Syntax:

os.listdir(path)

Parameters used:

This parameter specifies the path of the directories.

Code Snippet:

# Here, we are importing the OS module
import os
path = "D:\Stekies"
dir_list = os.listdir(path)
print("All the files and directories of this path are in here '", path, "' :")
# This will display all files in this directory
print(dir_list)

Output:

Explanation:

Look at the above output and see what types of files the method displayed in the console. The os.listdir() method of the Os Module returns the list of files and folders that the directories we mentioned have, and all these are of different file extensions.

Method 2: Using the os.listdir() to display only the files and no folders

Users can also use the os.listdir() method to retrieve the files with their file extensions mentioned with the names.

Code Snippet

import os
print("Using the Python os.listdir() method to print the list the files in the directory.")
d = input(r" Here, you enter your path of the folder and press enter: ")
print(f"Files in the directory: {d}")
f = os.listdir(d)
f = [f for f in f if os.path.isfile(d+'/'+f)] #This will filter only the files in the directory.
print(*f, sep="\n")

Output:

Explanation:

We used the os.listdir() method, which returns the files in the specified path, avoiding all the folders. In the output console, end-users will find an input statement where they enter their directory path.

After pressing enter, the method will display all the relevant files present in the directory.

Method 3: To retrieve only the particular files

The os.listdir() method also has a special characteristic. Users can use it to get the list of all particular files that users specify as file extensions.

Code Snippet:

# import OS
import os
for x in os.listdir('D:\Stekies'):
   if x.endswith(".png"):
      print(x)

Output:

Explanation:

Here, we used the same method to retrieve a particular file type, and we used the "png" file format. The endswith() method returns true if the string ends with a specified value, else returns false.

We specified the file extension as png, and it returned all the files with this file extension under the specified directory.

Note: When users use the os.getcwd(), it provides them with the current working directory, while the os.listdir lists the director.

Method 4: Using the os.walk() method

The os.walk() method generates those file names that exist in the directory by scanning the directories from top to bottom. It returns the file lists in a tree format by looping through all the directories in the system.

Syntax:

os.walk(top, topdown, onerror, followlinks)

Parameters used:

  • top: This parameter helps users to retrieve the top directory using which users want to get the list of the files and folders. Each directory rooted at directory yields 3-tuples, i.e., (dirpath, dirnames, filenames)
  • topdown: This parameter specifies that the method should walk the directories from top to bottom when the parameter is zero. And if users set the parameter to false, the method will scan from bottom to up.
  • onerror: This parameter provides an error handler if the method shows an error to continue with the scan or raise the exception to stop the scan.
  • followlinks: If users set this parameter to true, it visits directories pointed by system links.

Code Snippet:

import os
a = "D:\Stekies"
b = []
for (root, dirs, file) in os.walk(a):
   for x in file:
      if '.png' in x:
         print(x)

Output:

Method 5: Using the os.scandir() of the Os module in Python

This method returns an iterator of os.DirEntry objects corresponding to the files in the directory provided by the user.

Syntax:

os.scandir(path = '.')

Parameters used:

Here, the path parameter specifies the directories that users want to scan. By default, the method scans the current directory as the path parameter.

Code Snippet:

import os
a = "D:\Stekies"
b = os.scandir()
print("Here is the list of all files and directories in '% s':" % a)
for i in b:
   if i.is_dir() or i.is_file():
      print(i.name)

The glob module

Method 6: Using the glob() method of glob module

The glob() method of the glob module, short for a global module is another Python method that helps users find a specific file meeting the criteria of a particular pattern or name.

Code Snippet:

import glob
a = "D:\Stekies"
print('\n The name includes this wildcard *:')
for f in glob.glob(a + '*'):
   print(f)
print('\n The name includes this wildcard ?:')
for f in glob.glob(a + '?.txt'):
   print(f)
print('\n The name includes this wildcard ranges:')
for f in glob.glob(a + '/*[0-9].*'):
   print(f)

Output:

Explanation:

Here, the glob() method returns those files and folders that follow the specified pattern.

Method 7: To retrieve the files using the iglob() method

This method loops through all the files in the existing directory. If users set the parameter to true, this Python method will print the file names recursively.

Syntax:

glob.iglob(pathname, *, recursive=False)

Code Snippet:

import glob
a = "D:\Stekies\*.jpg"
print("\nUsing glob.iglob()")
for x in glob.iglob(a, recursive = True):
   print(x)

Output:

Explanation:

Here, we used the iglob() method to print all the files of the specified directory. Also, we explicitly mentioned the file type, i.e., the jpg file extension with the file path, which returns all the jpg files.

Conclusion:

File listing is one of the common approaches users use in Python, and there are several methods like os.listdir('dir_path'), os.scandir('path'), os.walk('dir_path'), and path.rglob('dir_path') of the Os and glob module.

This article highlighted all the points of the functionality of these methods with the respective code snippets and explanations.


×