In this article, we’ll look at how to write a Python program that lists subdirectories and files in the given path. It’s very important to perform this task efficiently when dealing with a large number of files and folders.
There are many ways to do this but we will look at the most efficient ways here.
os.walk
A quick and effective approach to list all subdirectories, and files in a folder is to use the os.walk function. For every directory it comes across, it provides a tuple that includes the current directory, a list of all subdirectories, and a list of all files.
Here is how to list every file and directory in a folder using the os.walk command:
import os
for root, dirs, files in os.walk('path/to/folder'):
for file in files:
print(os.path.join(root, file))
In the above code, the os.walk function recursively traverses all subdirectories of the specified folder and returns all files in those directories.
Here, we are using the os.path.join to get the the full path to each file printed.
glob.glob
There might be a cases where we need to get only the files with the specified pattern. For example, the files which starts with ‘x’ pattern or the files which are having the specific extensions. In such a scenario, the glob module provides a way to find all the files in a folder that match a specific pattern.
Here’s how to use glob.glob to list all files in a folder:
import glob
files = glob.glob('path/to/folder/*')
for file in files:
print(file)
The ‘*’ pattern is used to match all the files in the specified folder.
Please note that using the glob.glob can be more efficient than using os.walk when we only want to list files in a specific folder and not all subdirectories also when we have any specific requirements such as file name pattern & extension.
os.scandir
The os.scandir function provides an efficient way to list all files and directories in a folder. It returns a generator object that yields os.DirEntry objects for each file and directory in the folder.
Following is the example on how to use os.scandir to list all files and directories in a folder:
import os
with os.scandir('path/to/folder') as entries:
for entry in entries:
if entry.is_file():
print(entry.name)
In the above example program, the is_file() method of the os.DirEntry object returns True if the entry is a file, and False if it is a directory.