How to List All Files of a Directory in Python
Introduction
In Python, you may often come across situations where you need to list all the files present in a directory. This can be useful when you want to process all the files within a directory or perform some specific operations on each file. In this article, we will explore various methods to list all the files of a directory using Python.
Method 1: Using os module
The easiest way to list all files of a directory in Python is by using the os
module, which provides a range of functions for interacting with the operating system. Specifically, we can use the os.listdir()
function to get a list of all files and directories in a given path.
import os
# Specify the directory path
directory = '/path/to/directory'
# Get the list of all files and directories in the specified directory
file_list = os.listdir(directory)
# Print the file names
for file in file_list:
print(file)
The os.listdir()
function returns a list of all files and directories present in the specified directory. We can then iterate over this list and print each file name.
Example:
Let's say we have a directory called my_directory
with the following files:
- file1.txt
- file2.txt
- file3.py
- sub_directory
Using the above code, we can list all the files and directories of my_directory
as follows:
import os
directory = 'my_directory'
file_list = os.listdir(directory)
for file in file_list:
print(file)
The output will be:
file1.txt
file2.txt
file3.py
sub_directory
Method 2: Using glob module
Another way to list all files of a directory in Python is by using the glob
module. The glob
module provides a powerful function called glob.glob()
that can list all files matching a specific pattern within a directory.
import glob
# Specify the directory path
directory = '/path/to/directory'
# Get the list of all files in the specified directory
file_list = glob.glob(directory + '/*')
# Print the file names
for file in file_list:
print(file)
The glob.glob()
function returns a list of file paths (including the directory path) that match the specified pattern. In the above code, we use the '/*'
pattern to match all files within the directory.
Example:
Let's consider a directory called my_directory
which contains the following files:
- file1.txt
- file2.txt
- file3.py
- sub_directory
With the code provided above, we can list all the files within my_directory
as follows:
import glob
directory = 'my_directory'
file_list = glob.glob(directory + '/*')
for file in file_list:
print(file)
The output will be:
my_directory/file1.txt
my_directory/file2.txt
my_directory/file3.py
my_directory/sub_directory
Method 3: Using pathlib module (Python 3.4+)
Starting from Python 3.4, the pathlib
module was introduced as a high-level path library, providing a more object-oriented approach to file system paths. With the pathlib
module, we can also list all files of a directory.
import pathlib
# Specify the directory path
directory = '/path/to/directory'
# Create a Path object for the directory
path = pathlib.Path(directory)
# Get the list of all files in the specified directory
file_list = [file.name for file in path.iterdir() if file.is_file()]
# Print the file names
for file in file_list:
print(file)
In the above code, we create a Path
object by passing the directory path to the pathlib.Path()
constructor. Then, we use the iterdir()
method to iterate over all directories and files within the specified directory. Finally, we use the is_file()
method to filter out only the files and not the directories.
Example:
Let's assume we have a directory called my_directory
with the following files:
- file1.txt
- file2.txt
- file3.py
- sub_directory
Using the provided code, we can list all the files of my_directory
as follows:
import pathlib
directory = 'my_directory'
path = pathlib.Path(directory)
file_list = [file.name for file in path.iterdir() if file.is_file()]
for file in file_list:
print(file)
The output will be:
file1.txt
file2.txt
file3.py
Conclusion
In this article, we explored various methods to list all files of a directory using Python. We learned how to accomplish this using the os
module, the glob
module, and the pathlib
module. Now, you can easily obtain a list of all files in a directory and perform any required operations on them. Remember to choose the method that best suits your needs and the specific version of Python you are using.