How to List Files in a Directory with Python

Learn how to use the os and glob modules to list files in a directory with Python. This tutorial covers different methods, examples, and tips for working with files.

What is Directory?

A directory is a folder which a unit organizational structure in a computer’s for storing and locating files or more folders. Python now supports several APIs to list the directory contents.

List File in the Directory

Before we get into how to list files in directory using Python. We will see, what is a directory. A directory is a folder which a unit organizational structure in a computer’s for storing and locating files or more folders. Python now supports several APIs to list the directory contents.

Now, we need to learn how to list files in directory using Python from the path. We will be using Python to list the files in the directory. There are several methods:

  • OS module
  • glob module

Using the OS module to list the files in the directory

The OS module in Python provides the functionality for interacting with the OS(Operating System) like MAC OS, Windows, or Linux. This module comes under the Python standard utility modules. It provides an easy and efficient way to interact with the Operating system.

The OS module contains a lot of functionality:

  • checking the current directory(os.getcwd())
  • changing the directory(os.chdir())
  • copy, cut or paste the files from one directory to another(shutil.copytree())
  • listing the files in the directory(os.listdir())

Functions in OS to list the files in the directory:

  • os.listdir()
  • os.walk()
  • os.scandir()

os.listdir()

In this method os.listdir(), it gets the list of all files and directories in a specified directory. It does not return any files or folders. By default, it is the current directory.

Syntax:

os.listdir(path)

Parameters:

  • Path: It is the path to the directory.

Return Type:

It will return the list of all the files and folders in the given specified path.

Example 1: Getting all the lists in a directory

Code:

# code to list files in directory in python using os.listdir()
# import OS module
import os

path = "home/users/Desktop/folder"
dir_list = os.listdir(path)

print(f"Files and directories in '{path}' :")

print(dir_list)

Output:

Files and directories in 'home/users/Desktop/folder' :
['abc.csv','cde.csv', 'name.txt', 't.csv', 'main.py', 'util.py', 'index.html', 'random.txt']

Explanation:

In the above piece of code, we first import the OS module, and next, we have to define the path for which we want to get the files. Now, the next step will be to use the os.listdir() function that will take the input path and return the list of files available in the specified path. It will return a list of file names present in the directory.

Example 2: To get only .txt files

Code:

# code to list files in directory in python using os.listdir()
# import OS
import os

# Get the list of all files and directories
path = "home/users/Desktop/folder"

for x in os.listdir():
	if x.endswith(".txt"):
		# Prints only text files present in My Folder
		print(x)

Output:

name.txt
random.txt

Explanation:

In this piece of code, we will be using os.listdir() function to get only .txt files. we first import the OS module, and next, we have to define the path for which we want to get the files. Now, the next step will be to use the os.listdir() function that will take the input path and return the list of files available in the specified path. It will return a list of file names present in the directory. In the given list, we will use the for loop as each item of the list is a string, so by using the endswith() method of string, we will check any .txt file.

os.walk()

It returns the file names in the directory. This os.walk() function will return a list of files in the tree structure. The method loops through all of the directories in a tree.

Syntax:

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

Parameters:

top:

It is the top directory from which you want to retrieve the names of the component files and folders.

topdown:

It specifies that directories should be scanned from the top down when set to True. If this parameter is False, directories will be examined from the bottom up.

onerror:

It provides an error handler if an error is encountered.

followlinks:

If set to True, visits folders referenced by system links.

Return Type:

Returns the name of every file and folder within a directory and any of its subdirectories(list files in directory using Python).

Code:

# code to list files in directory in python using os.walk()
# import OS module
import os

path = "home/users/Desktop/folder"

l = []

# dirs=directories
for (root, dirs, file) in os.walk(path):
	for f in file:
		if '.csv' in f:
			print(f)
            l.append(f)

Output:

abc.csv
cde.csv
t.csv

Explanation:

For this code to get the files in the directory, we will use os.walk() function. First, we will import our OS module, as usual, and then we have to specify the directory path for which you want to get your files. Now we will use os.walk() in a for loop, this os.walk() function will return a tuple that contains the root of the path, the directory we are currently in and a list of the names of the file that we are receiving from the function. Now for all the files, we will print the file or for checking any specific file extension, we will be using the if statement for CSV files we will use .csv and return the list files in directory using Python with the .csv extension.

os.scandir()

Syntax:

os.scandir(path = '.')

Parameters:

  • Path: It is the path to the directory.

Return Type:

It returns an iterator of os.DirEntry object, and we get the list file in Directory using Python.

Note: os.DirEntry objects are intended to be used and thrown away after iteration as attributes and methods of the object cache their values and never refetch the values again. If the metadata of the file has been changed or if a long time has elapsed since calling os.scandir() method. we will not get up-to-date information.

Code:

# code to list files in directory in python using os.scandir()
# import OS module
import os

path = "home/users/Desktop/folder"

# Scan the directory and get
# an iterator of os.DirEntry objects
# corresponding to entries in it
# using os.scandir() method
o = os.scandir(path)

# List all files and directories in the specified path
print(f"Files and Directories in '{path}':")
for entry in o:
	if entry.is_dir() or entry.is_file():
		print(entry.name)

Output:

Files and Directories in 'home/users/Desktop/folder'
abc.csv
cde.csv
name.txt
t.csv
main.py
util.py
index.html
random.txt

List File in Directory Using Glob Module

In Python, the glob module is used to list files in a directory matching a specified pattern. The pattern rules of glob follow standard Unix path expansion rules. It is also predicted that according to benchmarks it is faster than other methods to match pathnames in directories.

iglob()

It is the method that can be used to print the list of files directory in Python recursively if the recursive parameter is set to True.

Syntax:

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

Parameters:

  • Pathname: It is the path to the directory.

Return type:

It will return the list file in the directory along with the path.

Code:

# code to list files in a directory in Python using iglob()
import glob

# This is my path
path = "home/users/Desktop/folder"


# Prints all types of files present in a Path
for file in glob.iglob(path, recursive=True):
	print(file)

Output:

home/users/Desktop/folder/abc.csv
home/users/Desktop/folder/cde.csv
home/users/Desktop/folder/name.txt
home/users/Desktop/folder/t.csv
home/users/Desktop/folder/main.py
home/users/Desktop/folder/util.py
home/users/Desktop/folder/index.html
home/users/Desktop/folder/random.txt

Explanation:

This piece of code is a bit different from other codes because this code uses the glob module instead of the os module, so first, we will import the glob module, and then specify the path of the directory. For getting the files in the directory, we will be using the glob.iglob function, which will return the path of the files.

#python 

How to List Files in a Directory with Python
2.25 GEEK