- Python List Files in a Directory
- Table of contents
- How to List All Files in a Directory using Python
- Example: List Files in a Directory
- Example 1: List only files in a directory
- Example 2: List both files and directories.
- os.walk() to list all files in directory and subdirectories
- os.scandir() to get files of a directory
- Glob Module to list Files of a Directory
- Pathlib Module to list files of a directory
- About Vishal
- Related Tutorial Topics:
- Python Exercises and Quizzes
- Python: Get list of files in directory sorted by name
- Get list of files in directory sorted by name using glob()
- Frequently Asked:
- Get list of files in directory sorted by names using os.listdir()
- Python: Get list of files in directory and sub-directories sorted by name
- Related posts:
Python List Files in a Directory
Also, there are multiple ways to list files in a directory. In this article, We will use the following four methods.
- os.listdir(‘dir_path’) : Return the list of files and directories in a specified directory path.
- os.walk(‘dir_path’) : Recursively get the list of all files in a directory and subdirectories.
- os.scandir(‘path’) : Returns directory entries along with file attribute information.
- glob.glob(‘pattern’) : glob module to list files and folders whose names follow a specific pattern.
Table of contents
How to List All Files in a Directory using Python
Use the listdir() and isfile() functions of an os module to list all files in a directory. Here are the steps.
- Import os module First, import the os module. This module helps us to work with operating system-dependent functionality in Python. The os module provides functions for interacting with the operating system.
- Decide the path to the directory Next, decide the path to the directory you want to list the files of. Make sure you use the correct format for your operating system. For example, on Windows, paths are typically written with backslashes (e.g., ‘C:\\path\\to\\your\\directory’ ).
- Use os.listdir(‘directory_path’) function Now use the os.listdir(‘directory_path’) function to get a list of the names of the entries ( the files and directories ) in the directory given by the directory_path .
- Iterate the result Next, Use for loop to Iterate the list returned by the os.listdir(directory_path) function. Using for loop we will iterate each entry returned by the listdir() function.
- Use isfile(path) function Now, In each loop iteration, use the os.path.isfile(‘path’) function to check whether the current entry is a file or a directory. If it is a file, then add it to a list. This function returns True if a given path is a file. Otherwise, it returns False.
Example: List Files in a Directory
Let’s see how to list all files in an ‘account’ folder.
Example 1: List only files in a directory
import os # directory/folder path dir_path = r'E:\account' # list to store files res = [] # Iterate directory for file_path in os.listdir(dir_path): # check if current file_path is a file if os.path.isfile(os.path.join(dir_path, file_path)): # add filename to list res.append(file_path) print(res)
Here we got three file names.
['profit.txt', 'sales.txt', 'sample.txt']
- The os.listdir(dir_path) will list files only in the current directory and ignore the subdirectories.
- The os.path.join(directory, file_path) is used to get the full path of the entry.
Also, you should handle the case where the directory does not exist or an error occurs while accessing it. For this, you can use a try-except block. Here’s how you can modify the code:
import os def list_files(dir_path): # list to store files res = [] try: for file_path in os.listdir(dir_path): if os.path.isfile(os.path.join(dir_path, file_path)): res.append(file_path) except FileNotFoundError: print(f"The directory does not exist") except PermissionError: print(f"Permission denied to access the directory ") except OSError as e: print(f"An OS error occurred: ") return res # directory/folder path dir_path = r'E:\account' files = list_files(dir_path) print('All Files:', files)
Generator Expression:
If you know generator expression, you can make code smaller and simplers using a generator function as shown below.
import os def get_files(path): for file in os.listdir(path): if os.path.isfile(os.path.join(path, file)): yield file
Then simply call it whenever required.
for file in get_files(r'E:\\account\\'): print(file)
Example 2: List both files and directories.
Directly call the listdir(‘path’) function to get the content of a directory.
import os # folder path dir_path = r'E:\\account\\' # list file and directories res = os.listdir(dir_path) print(res)
As you can see in the output, ‘reports_2021’ is a directory.
['profit.txt', 'reports_2021', 'sales.txt', 'sample.txt']
os.walk() to list all files in directory and subdirectories
The os.walk() function returns a generator that creates a tuple of values (current_path, directories in current_path, files in current_path).
Note: Using the os.walk() function we can list all directories, subdirectories, and files in a given directory.
It is a recursive function, i.e., every time the generator is called, it will follow each directory recursively to get a list of files and directories until no further sub-directories are available from the initial directory.
For example, calling the os.walk(‘path’) will yield two lists for each directory it visits. The first list contains files, and the second list includes directories.
Let’s see the example to list all files in directory and subdirectories.
from os import walk # folder path dir_path = r'E:\\account\\' # list to store files name res = [] for (dir_path, dir_names, file_names) in walk(dir_path): res.extend(file_names) print(res)
['profit.txt', 'sales.txt', 'sample.txt', 'december_2021.txt']
Note: Add break inside a loop to stop looking for files recursively inside subdirectories.
from os import walk # folder path dir_path = r'E:\\account\\' res = [] for (dir_path, dir_names, file_names) in walk(dir_path): res.extend(file_names) # don't look inside any subdirectory break print(res)
os.scandir() to get files of a directory
The scandir() function returns directory entries along with file attribute information, giving better performance for many common use cases.
It returns an iterator of os.DirEntry objects, which contains file names.
import os # get all files inside a specific folder dir_path = r'E:\\account\\' for path in os.scandir(dir_path): if path.is_file(): print(path.name)
profit.txt sales.txt sample.txt
Glob Module to list Files of a Directory
The Python glob module, part of the Python Standard Library, is used to find the files and folders whose names follow a specific pattern.
For example, to get all files of a directory, we will use the dire_path/*.* pattern. Here, *.* means file with any extension.
Let’s see how to list files from a directory using a glob module.
import glob # search all files inside a specific folder # *.* means file name with any extension dir_path = r'E:\account\*.*' res = glob.glob(dir_path) print(res)
['E:\\account\\profit.txt', 'E:\\account\\sales.txt', 'E:\\account\\sample.txt']
Note: If you want to list files from subdirectories, then set the recursive attribute to True.
import glob # search all files inside a specific folder # *.* means file name with any extension dir_path = r'E:\demos\files_demos\account\**\*.*' for file in glob.glob(dir_path, recursive=True): print(file)
E:\account\profit.txt E:\account\sales.txt E:\account\sample.txt E:\account\reports_2021\december_2021.txt
Pathlib Module to list files of a directory
From Python 3.4 onwards, we can use the pathlib module, which provides a wrapper for most OS functions.
- Import pathlib module: Pathlib module offers classes and methods to handle filesystem paths and get data related to files for different operating systems.
- Next, Use the pathlib.Path(‘path’) to construct directory path
- Next, Use the iterdir() to iterate all entries of a directory
- In the end, check if a current entry is a file using the path.isfile() function
import pathlib # folder path dir_path = r'E:\\account\\' # to store file names res = [] # construct path object d = pathlib.Path(dir_path) # iterate directory for entry in d.iterdir(): # check if it a file if entry.is_file(): res.append(entry) print(res)
Did you find this page helpful? Let others know about it. Sharing helps me continue to create free Python resources.
About Vishal
I’m Vishal Hule, Founder of PYnative.com. I am a Python developer, and I love to write articles to help students, developers, and learners. Follow me on Twitter
Related Tutorial Topics:
Python Exercises and Quizzes
Free coding exercises and quizzes cover Python basics, data structure, data analytics, and more.
- 15+ Topic-specific Exercises and Quizzes
- Each Exercise contains 10 questions
- Each Quiz contains 12-15 MCQ
Python: Get list of files in directory sorted by name
In this article, we will discuss different ways to get list of all files in a directory / folder sorted by name in python.
Table of contents
Get list of files in directory sorted by name using glob()
In python, the glob module provides a function glob() to find files in a directory based on matching pattern. Similar to the unix path expansion rules, we can use wildcards and regular expression to match & find few or all files in a directory. We will use this to get a list of all files in a directory and then sort that list of files by name. Steps are as follows,
- Get a list of all files or directories in a given directory using glob().
- Using the filter() function and os.path.isfileIO(), select files only from the list.
- Sort the list of files by name using sorted() function.
Complete example to get a list of all files in directory sorted by name is as follows,
import glob import os dir_name = 'C:/Program Files/Java/jdk1.8.0_191/include/' # Get list of all files in a given directory sorted by name list_of_files = sorted( filter( os.path.isfile, glob.glob(dir_name + '*') ) ) # Iterate over sorted list of files and print the file paths # one by one. for file_path in list_of_files: print(file_path)
Frequently Asked:
C:/Program Files/Java/jdk1.8.0_191/include\classfile_constants.h C:/Program Files/Java/jdk1.8.0_191/include\jawt.h C:/Program Files/Java/jdk1.8.0_191/include\jdwpTransport.h C:/Program Files/Java/jdk1.8.0_191/include\jni.h C:/Program Files/Java/jdk1.8.0_191/include\jvmti.h C:/Program Files/Java/jdk1.8.0_191/include\jvmticmlr.h
In this solution we created a list of files in a folder using globe() function. Then passed the list to filter() function to select only files from the list and skip dictionaries etc. For this we passed the os.path.isfile() function as an argument to the filter() function. Then we passed the list of files to the sorted() function, which returned a list of files in directory sorted by name.
But the list contains the complete paths of the files. What if we want only file names sorted by names?
Get list of files in directory sorted by names using os.listdir()
In Python, the os module provides a function listdir(dir_path), which returns a list of file and sub-directory names in the given directory path. Then using the filter() function create list of files only. Then sort this list of file names based on the name using the sorted() function.
Complete example to get list of files in directory sorted by name is as follows,
import os dir_name = 'C:/Program Files/Java/jdk1.8.0_191/include/' # Get list of all files in a given directory sorted by name list_of_files = sorted( filter( lambda x: os.path.isfile(os.path.join(dir_name, x)), os.listdir(dir_name) ) ) for file_name in list_of_files: print(file_name)
classfile_constants.h jawt.h jdwpTransport.h jni.h jvmti.h jvmticmlr.h
In this solution we created a list of file names in a folder sorted by name.
Python: Get list of files in directory and sub-directories sorted by name
In both the previous examples we created a list of files in a directory sorted by name. But it covered the files in the given directory only, not in nested directories. So, if you want to get a list of all files in directory and sub-directory sorted by name, then checkout this example,
import glob import os dir_name = 'C:/Program Files/Java/jdk1.8.0_191/include/' # Get list of all files in a given directory & sub-directories sorted by name list_of_files = sorted( filter( os.path.isfile, glob.glob(dir_name + '/**/*', recursive=True) ) ) # Iterate over sorted list of files and print the file paths # one by one. for file_path in list_of_files: print(file_path)
C:/Program Files/Java/jdk1.8.0_191/include\classfile_constants.h C:/Program Files/Java/jdk1.8.0_191/include\jawt.h C:/Program Files/Java/jdk1.8.0_191/include\jdwpTransport.h C:/Program Files/Java/jdk1.8.0_191/include\jni.h C:/Program Files/Java/jdk1.8.0_191/include\jvmti.h C:/Program Files/Java/jdk1.8.0_191/include\jvmticmlr.h C:/Program Files/Java/jdk1.8.0_191/include\win32\bridge\AccessBridgeCallbacks.h C:/Program Files/Java/jdk1.8.0_191/include\win32\bridge\AccessBridgeCalls.c C:/Program Files/Java/jdk1.8.0_191/include\win32\bridge\AccessBridgeCalls.h C:/Program Files/Java/jdk1.8.0_191/include\win32\bridge\AccessBridgePackages.h C:/Program Files/Java/jdk1.8.0_191/include\win32\jawt_md.h C:/Program Files/Java/jdk1.8.0_191/include\win32\jni_md.h
We used the glob() function with pattern ‘/**/*’ and recursive=True argument. It gave a list of all files, sub- directories and files in the sub-directories. Then using the filter() function and os.path.isfile() we filtered the files only and skipped the directories. Then using the sorted() function, sorted these filtered files and created a list of files sorted by name.
We learned about different ways to get a list of files in a folder, sorted by name.