Python List All Files In Directory And Subdirectories
Last Updated :
24 Apr, 2025
Listing all files in a directory and its subdirectories is a common task in Python, often encountered in file management, data processing, or system administration. As developers, having multiple approaches at our disposal allows us to choose the most suitable method based on our specific requirements. In this article, we will see how we can list all files in a directory and subdirectories in Python.
List All Files In Directory And Subdirectories in Python
Below are some of the ways by which we can list all files in directory and subdirectories in Python:
List All Files In Directory And Subdirectories Using os.listdir and Recursion
In this example, the Python function `list_files_recursive` recursively traverses a specified directory (`'./'` by default), printing the full paths of all files within that directory and its subdirectories. The function uses the `os` module to list and navigate directories.
Python
import os
def list_files_recursive(path='.'):
for entry in os.listdir(path):
full_path = os.path.join(path, entry)
if os.path.isdir(full_path):
list_files_recursive(full_path)
else:
print(full_path)
# Specify the directory path you want to start from
directory_path = './'
list_files_recursive(directory_path)
Output:
.png)
List All Files In Directory And Subdirectories Using os.walk
In this example, the Python function `list_files_walk` recursively traverses a specified directory (`'./'` by default) using `os.walk`, printing the full paths of all files within that directory and its subdirectories. The function takes advantage of the `os` module's `walk` function, which efficiently generates file paths during directory traversal.
Python
import os
def list_files_walk(start_path='.'):
for root, dirs, files in os.walk(start_path):
for file in files:
print(os.path.join(root, file))
# Specify the directory path you want to start from
directory_path = './'
list_files_walk(directory_path)
Output:
.png)
List All Files In Directory And Subdirectories Using glob Module
In this example, the Python function `list_files_glob` uses the `glob` module to list and print the full paths of files matching a specified pattern (`'./**/*'` by default). The `recursive` parameter is set to `True` by default, allowing it to recursively search subdirectories. The function demonstrates a concise way to obtain file paths using globbing patterns.
Python3
import glob
def list_files_glob(pattern='./**/*', recursive=True):
files = glob.glob(pattern, recursive=recursive)
for file in files:
print(file)
list_files_glob()
Output:
.png)
List All Files In Directory And Subdirectories Using os.scandir
In this example, the Python function `list_files_scandir` utilizes the `os.scandir` method to efficiently iterate over entries (files and directories) in a specified directory (`'./'` by default). It prints the full paths of files and recursively explores subdirectories using `os.scandir`'s context manager, enhancing performance compared to using `os.listdir` for large directories.
Python3
import os
def list_files_scandir(path='.'):
with os.scandir(path) as entries:
for entry in entries:
if entry.is_file():
print(entry.path)
elif entry.is_dir():
list_files_scandir(entry.path)
# Specify the directory path you want to start from
directory_path = './'
list_files_scandir(directory_path)
Output:
.png)
List All Files In Directory And Subdirectories Using pathlib Module
In this example, the Python function `list_files_pathlib` utilizes the `pathlib` module to list and print the paths of files and directories in a specified directory (`'./'` by default). It uses the `iterdir` method to iterate over entries, and if the entry is a file, its path is printed; if it's a directory, the function recursively explores its contents. The use of `pathlib` provides a more object-oriented and readable way to work with file paths.
Python3
from pathlib import Path
def list_files_pathlib(path=Path('.')):
for entry in path.iterdir():
if entry.is_file():
print(entry)
elif entry.is_dir():
list_files_pathlib(entry)
# Specify the directory path you want to start from
directory_path = Path('./')
list_files_pathlib(directory_path)
Output:
.png)
Video Demonstration
Considerations and Best Practices:
When it comes to choosing the best approach for listing files in a directory and its subdirectories, consider the following best practices:
- Readability and Maintainability: Choose an approach that aligns with the readability and maintainability of your code. Code clarity is crucial for collaboration and future modifications.
- Python Version Compatibility: Be aware of the Python version you are using. While older versions may not support the newer modules like pathlib and os.scandir, the os module remains a reliable choice across various Python versions.
- Use os.walk for Simplicity: If simplicity is a priority, especially for basic use cases, the os.walk method is a straightforward and widely used solution. It provides a simple interface for traversing directories and listing files.
- Consider pathlib for Modern Code: If you are working with Python 3.4 or newer, consider using the pathlib module. It offers a more modern and object-oriented approach to file and path manipulation, enhancing code readability and usability.
- Performance Considerations: For large directory structures, performance may be a consideration. In such cases, test different approaches and choose the one that provides the best performance for your specific use case.
Conclusion:
In conclusion, Python offers developers a diverse set of tools to tackle the task of listing files in a directory and its subdirectories. The choice of approach depends on factors such as code readability, Python version compatibility, and specific use case requirements. Whether you opt for the simplicity of os.walk, the flexibility of glob, the efficiency of os.scandir, or the modernity of pathlib, each method empowers you to handle this common task with confidence. By understanding these approaches, you can enhance your coding skills and choose the best tool for the job at hand.
Similar Reads
Python - List Files in a Directory
Sometimes, while working with files in Python, a problem arises with how to get all files in a directory. In this article, we will cover different methods of how to list all file names in a directory in Python.Table of ContentWhat is a Directory in Python?How to List Files in a Directory in PythonLi
8 min read
How to List all Files and Directories in FTP Server using Python?
FTP ( File Transfer Protocol ) is set of rules that computer follows to transfer files across computer network. It is TCP/IP based protocol. FTP lets clients share files. FTP is less secure because of files are shared as plain text without any encryption across the network. It is possible using pyt
2 min read
List all files of certain type in a directory using Python
In python, there are several built-in modules and methods for file handling. These functions are present in different modules such as os, glob, etc. This article helps you find out many of the functions in one place which gives you a brief knowledge about how to list all the files of a certain type
3 min read
Python - Get list of files in directory sorted by size
In this article, we will be looking at the different approaches to get the list of the files in the given directory in the sorted order of size in the Python programming language. The two different approaches to get the list of files in a directory are sorted by size is as follows: Using os.listdir(
3 min read
Python - Get list of files in directory with size
In this article, we are going to see how to extract the list of files of the directory along with its size. For this, we will use the OS module. OS module in Python provides functions for interacting with the operating system. OS comes under Pythonâs standard utility modules. This module provides a
9 min read
How to Recursively Grep all Directories and Subdirectories in Linux
The grep command is one of the most frequently used utilities in Linux for searching through text files. It allows users to search for specific strings within files and directories, offering a wide range of options to customize the search process. In this article, weâll explore how to use the recurs
5 min read
Listing out directories and files in Python
The following is a list of some of the important methods/functions in Python with descriptions that you should know to understand this article. len() - It is used to count number of elements(items/characters) of iterables like list, tuple, string, dictionary etc. str() - It is used to transform data
6 min read
Python - List files in directory with extension
In this article, we will discuss different use cases where we want to list the files with their extensions present in a directory using python. Modules Usedos: The OS module in Python provides functions for interacting with the operating system.glob: In Python, the glob module is used to retrieve fi
3 min read
Shell Script to List all Hidden Files in Current Directory
Here we are going to see how to write a script to list all hidden files in the current directory, But before starting we will see how to hide the file in the current directory. Hide Files in Linux: In Linux, the files which start with a period (.) sign are the hidden files. We will write a small scr
2 min read
Copy And Replace Files in Python
In Python, copying and replacing files is a common task facilitated by modules like `shutil` and `os`. This process involves copying a source file to a destination location while potentially replacing any existing file with the same name. Leveraging functions like `shutil.copy2` and `os.remove`, thi
2 min read