In this article, we will discuss multiple methods to list all the files in a directory using Python. A directory is a folder that contains files or other directories. In Python, we can use various methods to list all the files in a directory.
Method 1: Using os.listdir()
The os module provides a function called os.listdir()
that returns a list containing the names of the files and directories in the specified directory. We can use this function to list all the files in a directory.
import os # Specify the directory path path = "/path/to/directory" # Get the list of all files and directories in the specified directory files = os.listdir(path) # Print the list of files for file in files: print(file)
In the code above, we first import the os
module. We then specify the directory path using a string. We use the os.listdir()
function to get the list of all files and directories in the specified directory. We then use a for loop to print the list of files.
Method 2: Using os.scandir()
The os module provides another function called os.scandir()
that returns an iterator over the files and directories in the specified directory. We can use this function to list all the files in a directory.
import os # Specify the directory path path = "/path/to/directory" # Get the iterator over the files and directories in the specified directory with os.scandir(path) as entries: # Iterate over the entries for entry in entries: # Check if the entry is a file if entry.is_file(): print(entry.name)
In the code above, we first import the os
module. We then specify the directory path using a string. We use the os.scandir()
function to get an iterator over the files and directories in the specified directory. We then use a for loop to iterate over the entries in the iterator. We check if each entry is a file using the is_file()
method, and if it is a file, we print its name.
Method 3: Using glob.glob()
The glob
module provides a function called glob.glob()
that returns a list of filenames that match a specified pattern. We can use this function to list all the files in a directory.
import glob # Specify the directory path path = "/path/to/directory/*" # Get the list of all files in the specified directory files = glob.glob(path) # Print the list of files for file in files: print(file)
In the code above, we first import the glob
module. We then specify the directory path using a string and a wildcard character (*). We use the glob.glob()
function to get the list of all files in the specified directory. We then use a for loop to print the list of files.
Method 4: Using pathlib.Path()
The pathlib module provides a class called Path
that represents a path to a file or directory. We can use this class to list all the files in a directory.
import pathlib # Specify the directory path path = pathlib.Path('/path/to/directory') # Get the list of all files in the specified directory files = [file.name for file in path.iterdir() if file.is_file()] # Print the list of files for file in files: print(file)
In the code above, we first import the pathlib
module. We then specify the directory path using a Path
object. We use the iterdir()
method of the Path
object to get an iterator over the files and directories in the specified directory. We then use a list comprehension to get a list of all the files in the directory. We check if each entry in the iterator is a file using the is_file()
method, and if it is a file, we add its name to the list. Finally, we use a for loop to print the list of files.
Method 5: Using os.walk()
The os module provides a function called os.walk()
that generates the file names in a directory tree by walking the tree either top-down or bottom-up. We can use this function to list all the files in a directory.
import os # Specify the directory path path = "/path/to/directory" # Walk the directory tree and get the list of all files for root, directories, files in os.walk(path): for file in files: print(os.path.join(root, file))
In the code above, we first import the os
module. We then specify the directory path using a string. We use the os.walk()
function to walk the directory tree and get the list of all files. We use a nested for loop to iterate over the files in each directory and print their full path using the os.path.join()
method.
Conclusion
In this article, we discussed multiple methods to list all the files in a directory using Python. We covered five different methods: os.listdir()
, os.scandir()
, glob.glob()
, pathlib.Path()
, and os.walk()
. Each method has its own advantages and disadvantages, so it’s important to choose the method that best suits your specific use case.