In any programming language, being able to list files in a directory is an essential feature. You can use Python’s built-in modules, such as “os” and “pathlib” to accomplish this task (listing files in a directory).
This blog post explains various Python methods with examples to list files in a directory.
- Method 1: Using os.listdir() Function
- Method 2: Using os.scandir() Function
- Method 3: Using os.walk() Function
- Method 4: Using glob.glob() Function
- Method 5: Using pathlib Module
Method 1: Using os.listdir() Function
The “os.listdir()‘ function is used to get the files and directories of the given specified path. Here is an example of code:
The following files are present in the given specific directory:
Code:
import os
orginal_path = r'C:\Users\p\Documents\program'
all_file = []
for file in os.listdir(orginal_path):
if os.path.isfile(os.path.join(orginal_path, file)):
all_file.append(file)
print(all_file)
In the above code, the “os” module is imported, and the empty list is created where the file names will be stored. The “for loop” is used along with the ‘if-statement “ to iterate over the list of directories and append the file’s name to the empty list.
Output:
The above output shows the names of the available files placed in the specified directory.
Method 2: Using os.scandir() Function
In Python, the “os.scandir()” function is used to get the file names along with attribute information. Here is an example code:
Code:
import os
dir_path = os.scandir("C:/Users/p/Documents/program")
for file in dir_path:
if file.is_dir() or file.is_file():
print(file.name)
In the above code, the “os.scandir()” function takes the complete path as an argument, and it will return all the files in the specified directory. The “for” loop iterates over all the files available in the specified path and returns the file/directory names using the “file.is_dir()” or “file.is_file()” functions.
Output:
The above output shows the name of the files placed in the targeted directory.
Method 3: Using os.walk() Function
The os.walk() function is used to generate the names of files from the given directory tree by walking the tree. In the code given below, the “os.walk()” function is used along with for loop to list all the names of files in the given directory:
Code:
import os
for root, directory, files in os.walk('C:/Users/p/Documents/program'):
for filename in files:
print(os.path.join(root, filename))
The nested for loop iterates on all the files in the directory and the “os.path.join()” function is utilized to join the root directory with the file name to generate the full file path.
Output:
The above output shows the file’s name in the specified directory.
Method 4: Using glob.glob() Function
In the example code given below, the “glob()” function is used along with the “for” loop to list files of a specific directory.
Code:
import glob
dir_path = r'C:/Users/p/Documents/program\**\*.*'
for file in glob.glob(dir_path, recursive=True):
print(file)
In the above code, the “for” loop iterates over the list of directories and returns the file name. The recursive argument is set to True to search for files in subdirectories.
Output:
The above snippet shows the file’s name in the specified directories.
Method 5: Using pathlib Module
In the example code given below, the “pathlib.path()” and “pathlib.iterdir()” functions of the “pathlib” module are used to list all the files from the specified directory:
Code:
import pathlib
dir_path = pathlib.Path("C:/Users/p/Documents/program")
for file in dir_path.iterdir():
if file.is_file():
print(file)
In the above code, the “pathlib.path()” accepts the original path of the file and stores it in a variable “dir_path”. The “for loop” iterates over the path using the “iterdir()” method to iterate over all the files and subdirectories in the directory. The “file.is_file()” checks the file’s existence, and if the file is present in the directory, the print() function prints the file name.
Output:
The above output shows the file’s name of the selected directory.
Conclusion
To list files in a directory, the “os.listdir()” function, “os.scandir()”, “os.walk()” function, “glob.glob()” function, and “pathlib” module is used in Python. The “os.listdir()” function is used to get the file’s name and directory in the given directory. Similarly, the “os” module functions, such as the “os.scandir()” and “os.walk()”, are used to list files in the directory. This guide provided a complete explanation of how to list files in a directory in Python.