In this post, we will see how to list all files in a directory in Python.
There are multiple ways to list all files in a directory in Python.
Table of Contents
Using os.walk
Python os module provides multiple function to get list of files.
List all files in directory and its subdirectories using os.walk(path).
It iterates directory tree in path and for each directory, it returns a tuple with (,
)
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
import os path = '/users/apple/temp/' # create empty List listOfFiles = list() for( directory, subdirectories, file ) in os.walk(path): for f in file: listOfFiles.append(os.path.join(directory,f)) for file in listOfFiles: print(file) |
Output:
/users/apple/temp/sample2.txt
/users/apple/temp/images/image1.jpeg
/users/apple/temp/images/image2.jpeg
List all .jpeg files in directory and its subdirectories using os.walk(path).
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
import os path = '/users/apple/temp/' # create empty List listOfFiles = list() for( directory, subdirectories, file ) in os.walk(path): for f in file: if '.jpeg' in f: listOfFiles.append(os.path.join(directory,f)) for file in listOfFiles: print(file) |
Output:
/users/apple/temp/images/image2.jpeg
Using os.listdir(path)
You can also use os.listdir(path)to get list of files and subdirectories in a directory. In case, we get subdirectory while iterating, then we call getListOfFiles(path) recursively.
Here is the implementation of recursive function getListOfFiles(path).
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
def getListOfFiles(path): listOfFilesInDir = os.listdir(path) listOfAllFiles = list() # Iterate over all the files for obj in listOfFilesInDir: # get absolute path absolutePath = os.path.join(path, obj) # check if absolutePath is path of directory. If yes, then call the function recursively if os.path.isdir(absolutePath): listOfAllFiles = listOfAllFiles + getListOfFiles(absolutePath) else: listOfAllFiles.append(absolutePath) return listOfAllFiles |
Call the above function by passing path.
|
1 2 3 4 5 6 7 8 |
path = '/users/apple/temp/' # Get the list of all files in directory and its subdirectories listOfFiles = getListOfFiles(path) for f in listOfFiles: print(f) |
/users/apple/temp/sample2.txt
/users/apple/temp/images/image1.jpeg
/users/apple/temp/images/image2.jpeg
Using glob
You can use glob module to list files in directory and its subdirectory. It supports recursive globs using **.
List all files and subdirectories in a directory using glob.glob().
|
1 2 3 4 5 6 7 8 9 10 |
import glob path = '/users/apple/temp/' listOfFiles = [f for f in glob.glob(path + "**/*", recursive=True)] for file in listOfFiles: print(file) |
/users/apple/temp/sample2.txt
/users/apple/temp/images/image1.jpeg
/users/apple/temp/images/image2.jpeg
List all .txt files in directory and its subdirectory
|
1 2 3 4 5 6 7 8 9 10 |
import glob path = '/users/apple/temp/' listOfFiles = [f for f in glob.glob(path + "**/*.txt", recursive=True)] for file in listOfFiles: print(file) |
Output:
/users/apple/temp/sample2.txt
List name of all subdirectories in a directory.
|
1 2 3 4 5 6 7 8 9 10 |
import glob path = '/users/apple/temp/' listOfFiles = [f for f in glob.glob(path + "**/*", recursive=True)] for file in listOfFiles: print(file) |
Output:
/users/apple/temp/images
Conclusion
We have learnt 3 ways to list all files in a directory in Python. We have also learnt about filtering files on the basis of extension.