There can be many cases where you need to list all files in the Directory in Python. Whether it is data files, code files, or any other file type, listing all the files in a directory can be a quite useful operation.
Python has a lot of libraries to list all files and directories in a directory. In this article, We used os, glob
and path
modules to list files in a directory and filter based on the file type, and file name and listed down ways to list files and subdirectories recursively.
Quick Summary
We recommend you choose one of the following methods based on your requirements.
Simple files and directories listing:
If you are looking simple way of listing files and subdirectories in a directory then the following methods will work fine.
– Using listdir()
from the os
library
File metadata required:
If searching for a way to get files and subdirectories along with access to metadata then the below methods work well.
– Using scandir()
from os
library – returns a list of DirEntry objects.
– Using iterdir()
from the path
library – returns a list of Path objects.
– Using glob()
from the path
library – returns a list of Path objects.
Recursive Listing:
The following methods list entries of a folder recursively.
– Using glob()
from the glob
module
– Using rglob()
from the path
module
– Using walk()
from os
module
File names starting with dot(.):
Most of the below methods do not get files and subdirectories if the name starts with a dot(.) but the below ones do.
Using the glob()
from the path
module
Using the rglob()
from the path
module
Pattern matching support:
If you need methods to list only certain types of files, then the following methods support conditional listing. For example, list only *.log files.
– Using glob()
from the glob
module
– Using glob()
from the path
module
– Using rglob()
from the path
module
For all the below methods, we will use test
folder as input. As shown in the below images, test
directory is having 4 files and 2 subfolders.
- a.txt: file
- b.txt: file
- c.txt: file
- python: folder
- .txt: file
- .test1: folder – empty folder
![Python List All Files in Directory [5 Best Methods] 3 list of files in a test directory](https://codethreads.dev/wp-content/uploads/2022/12/test_content_folders.png)
And the subfolder python
is having the below 3 files in it.
- d.txt: file
- e.txt: file
- f.txt: file
![Python List All Files in Directory [5 Best Methods] 4 list of files in python directory](https://codethreads.dev/wp-content/uploads/2022/12/python_folder_contents-1.png)
Python List All Files and Directories in a Directory
We have listed down 5 methods to list all files and subdirectories in a directory.
- All the methods return both files and subfolders
- Does not work recursively and returns files and folders from one direct level only.
Skip to other sections directly, if you are either looking for only files listing or recursive listing of files in a directory.
Method 1: Using listdir() from os library
The listdir()
function from os
library takes path
parameter and returns a list of subfolders and file names. We can use this method to get all the files in a directory in Python.
Notes:
- Path: String – The path parameter is optional, when you don’t pass the path parameter, the default value will be the current path(‘.’)
- The listdir() returns both folders and file names from one direct level only, not recursively.
- Returns only string names but not the other file information.
Code Thread:
import os
path = '/home/user/Desktop/test'
# listdir method lists both files and folders
all_entries = os.listdir(path)
print(all_entries)
Output:
['b.txt', 'c.txt', 'a.txt', 'python']
Method 2: Use glob() from glob module
The glob()
method from glob
module can be used to list files and directories of a directory. Following are the noteworthy points about glob()
method.
- It takes a path pattern as an input parameter and a boolean flag named recursive.
- Returns a list of full path strings that matched the given path pattern
- Internally it wraps the
iglob()
method which returns an iterator of strings - The
glob()
method does not match the files and folders starting with dot(.) - Returns only full path string but not the other file meta information.
Code Thread:
import glob
print(glob.glob('/home/user/Desktop/test/*'))
Output:
['/home/user/Desktop/test/b.txt', '/home/user/Desktop/test/c.txt', '/home/user/Desktop/test/a.txt', '/home/user/Desktop/test/python']
Code Thread for files listing in folder with relative paths:
You can use os.relpath()
to extract relative paths of files.
import glob
all_entries = glob.glob('/home/user/Desktop/test/*')
rel_entries = [os.path.relpath(entry, path) for entry in all_entries]
print(rel_entries)
Output:
['b.txt', 'c.txt', 'a.txt', 'python']
Method 3: Use scandir() from os module
Alternatively, You can use scandir()
from os module to list all files in a folder in Python.
- Takes a path parameter as input and returns an iterator object with all the directory entries.
- As it returns a list of
DirEntry
objects, you will be able to access other file information as well. Following are a few important properties of aDirEntry
object.- name: returns the name of the file
- path: returns the path of the file
- stat(): returns an object with file properties like size, permissions/mode, access timestamps
- is_file(): returns True when the given entry is a file otherwise False
- is_directory(): returns True when the given entry is a folder otherwise False.
Code Thread:
import os
path = '/home/user/Desktop/test'
files = [file.name for file in os.scandir(path)]
print(files)
Output:
['b.txt', 'c.txt', 'a.txt', 'python']
Method 4: Using Path.iterdir() from pathlib module
Similar to scandir()
, iterdir()
method also returns an iterator of DirEntry
objects which contain both files and folders. Following are a few important points about iterdir()
method.
- It does not return files and folders whose names start with the dot(.)
- Returns the FileNotFoundError when an invalid path is given.
- Unlike the glob() function, it does not support pattern-matching paths.
Code Thread:
import pathlib
path = '/home/user/Desktop/test'
all_entries = [entry.name for entry in pathlib.Path(path).iterdir()]
print(all_entries)
Output:
['b.txt', 'c.txt', 'a.txt', 'python']
Method 5: Using Path.glob() from pathlib module
Like scandir()
and glob()
method from the glob module, this method also returns an iterator of Path objects. In addition, it also supports pattern-matching paths.
This method does not return an error when the unknown path is given, instead, it returns an empty iterator.
Code Thread:
import pathlib
path = '/home/user/Desktop/test'
all_entries = [entry.name for entry in pathlib.Path(path).glob('*')]
print(all_entries)
Output:
['b.txt', 'c.txt', 'a.txt', 'python']
Python List only files in a Directory
All the solutions from the above section list both files and folders from a directory. In this section, we will go through methods that list only files in a directory.
Method 1: Using listdir() from os library
The listdir() function from the os
library returns both files and folder names as a list of strings. Hence it is difficult to find out whether the given name represents a file or a directory.
We will filter out files with the below steps:
- Step 1: Build the full path of the file, with
os.path.join()
method which takes a base path and file name, concatenates them, and returns the full path. - Step 2: Use
os.path.isfile()
method with the full path passed as a parameter, to know whether the path represents a file or a folder.
Code Thread:
import os.path
path = '/home/user/Desktop/test'
# os.listdir to get all files and folder in given path
# os.path.file(path) returns True if path represents file
# os.path.join(path1, path2) it concatenates path, file to get full path
all_files = os.listdir(path)
only_files = [file for file in all_files if os.path.isfile(os.path.join(path, file))]
print(only_files)
Output:
['b.txt', 'c.txt', 'a.txt']
Method 2: Use glob() from glob module
Alternatively, You can also use the glob()
function from the glob
module to list only files in a folder. This method also returns list of strings of full paths, we will have to use os.path.isfile()
to filter the files.
This method returns full paths, use os.path.relpath(file_path, base_path)
to get the file names.
Code Thread:
import glob
import os.path
current_path = '/home/user/Desktop/test'
all_entries = [ entry for entry in glob.glob(current_path+'/**')]
all_files = [entry for entry in all_entries if os.path.isfile(entry)]
print(all_files)
Output:
['/home/user/Desktop/test/b.txt', '/home/user/Desktop/test/c.txt', '/home/user/Desktop/test/a.txt']
Method 3: scandir() from os library
Like listdir()
function, scandir()
also returns a list of files and folders but as an iterator of DirEntry
objects. The DirEntry
object has useful properties to fetch information about a particular entry. Following are a few such properties.
- is_file()
- is_dir()
- stat()
- name
We can use isfile()
method to know whether the entry is a file or a directory.
Code Thread:
import os
path = '/home/user/Desktop/test'
files = [file.name for file in os.scandir(path) if file.is_file()]
print(files)
Output:
['b.txt', 'c.txt', 'a.txt']
Method 4: Using iterdir() from path module
Similar to scandir()
from os module, iterdir()
returns a list of files and directories in a folder as an iterator of Path
objects. We can use is_file() method of Path to identify whether the entry is a file or a folder.
Code Thread:
import pathlib
path = '/home/user/Desktop/test'
all_entries = [entry.name for entry in pathlib.Path(path).iterdir() if entry.is_file()]
print(all_entries)
Output:
['b.txt', 'c.txt', 'a.txt']
Method 5: Use glob() from path module
The glob()
from path
module is similar to iterdir()
in returning files and folders as an iterator of Path
objects. As in the above method, we can is_file() to filter out only files.
Code Thread:
import pathlib
path = '/home/user/Desktop/test'
all_entries = [entry.name for entry in pathlib.Path(path).glob('*') if entry.is_file()]
print(all_entries)
Output:
['b.txt', 'c.txt', 'a.txt']
Python List all files in a directory recursively
So far, we have seen methods to list all files and folders in a directory and methods to list only files in a directory. As you have observed, all these methods do not list files and folders from subfolders, just list entries from the one direct level.
Now we will see solutions to list all files in a folder recursively.
Method 1. Use the glob() from glob module
The glob()
function from the glob
module takes recursive
parameter which decides whether the function returns files and subfolders recursively or not.
Notes: 1. glob() supports path patterns for conditional scanning of a folder. 2. It returns empty iterator when invalid path is given 3. The glob() method does not return files and folders whose name is starting with dot '.'
Code Thread:
import glob
path = '/home/user/Desktop/test/'
pattern = '**'
all_entries = glob.glob(path+pattern, recursive=True)
all_files = [entry for entry in all_entries if os.path.isfile(os.path.join(path, entry))]
all_folders = [entry for entry in all_entries if os.path.isdir(os.path.join(path, entry))]
print(all_files)
print(all_folders)
Output:
['/home/user/Desktop/test/b.txt', '/home/user/Desktop/test/c.txt', '/home/user/Desktop/test/a.txt', '/home/user/Desktop/test/python/f.txt', '/home/user/Desktop/test/python/e.txt', '/home/user/Desktop/test/python/d.txt']
['/home/user/Desktop/test/', '/home/user/Desktop/test/python']
Method 2. Using Path.rglob() from pathlib module
Alternatively, You can also use rglob()
method from the pathlib
module to list the entries recursively. Following are the noteworthy points about rglob() method.
Notes: 1. rglob() function supports path pattern for conditional listing. Path pattern is based on Unix wildcard commands. 2. Does not return any error if given path is incorrect but returns empty iterator. 3. It even returns files and folders that starts with dot(.)
Code Thread:
import pathlib
path = '/home/user/Desktop/test'
all_entries = [entry for entry in pathlib.Path(path).rglob('*')]
all_files = [entry.name for entry in all_entries if entry.is_file()]
all_folders = [entry.name for entry in all_entries if entry.is_dir()]
print(all_files)
print(all_folders)
Output:
['.txt', 'b.txt', 'c.txt', 'a.txt', 'f.txt', 'e.txt', 'd.txt']
['.test1', 'python']
Method 3: walk() from os module
The walk()
method from the os
module takes the string path as an input parameter and returns the below three details for the given root folder and all the subfolders.
- root: string – root folder path
- directories: list(str) – all directory names from the root directory
- files: list(str) – all file names from the root directory
Notes: 1. Does not support conditional listing based on Path patterns 2. Returns empty iterator when incorrect path is given.
Code Thread:
import os
path = '/home/user/Desktop/test'
all_entries = [files + directories for root, directories, files in os.walk(path)]
all_entries_flattened = []
for entries in all_entries:
all_entries_flattened.extend(entries)
print(all_entries_flattened)
Output:
['b.txt', 'c.txt', 'a.txt', 'python', 'f.txt', 'e.txt', 'd.txt']
List files in directory with pattern matching
The methods mentioned below will help you if you’re searching for a way to list all files of a particular type in a folder. Following are a few cases where the below methods will be useful.
- List all files ending with a certain keyword in a folder, ex: ‘.txt’ files in a folder, ‘.csv’ files in a folder
- List all files starting with a particular name, ex: all files starting with ‘20221226’
- List all files containing a word in a folder, ex: all files containing the ‘python’ word
In short, below 2 methods works with unix wildcard characters.
Wildcard | Description | Wildcard String | Matching Examples | Non-Matching Examples |
? | Matches with only one character | a? | aa, ab, ac, a1, a! | aaa, ba |
* | Matches with zero or more characters | a* | a, aa, aaa, abb, aaad, aa12 | baaa, eaaa, da |
[] | matches with characters mentioned with the brackets [] | b[amd]t | bat, bmt, and bdt | bamt, badt |
- Other useful wildcards – {}, [!] and \
Method 1: Using glob() from glob module
Below is an example to get list files ending with ‘.txt’ in a directory. Similarly you can use ‘*.csv’ for getting only files ending with ‘.csv’.
Notes: 1. Theglob.glob()
method does not list files recursively, as recursive is False by default. If you need recursive listing then need to send recursive parameter as True. 2. This method returns both files and folders, Useos.path.isfile(os.path.join(current_path, entry)
to get only files. 3. glob() method returns full paths of files, To get only file names useos.path.relpath(entry, path)
Code Thread:
import glob
path = '/home/user/Desktop/test/'
pattern = '*.txt'
all_entries = glob.glob(path + pattern)
print(all_entries)
Output:
['/home/user/Desktop/test/b.txt', '/home/user/Desktop/test/c.txt', '/home/user/Desktop/test/a.txt']
Method 2: Use glob() from path module
Similarly we can glob()
function from path
module to list only particular types of files and folders in a directory.
Notes: 1. glob() function path module does not list files recursively. You need to use rglob() function to list files recursively. 2. This method lists both files and subfolders, use entry.is_file() method to list only files.
Code Thread:
import pathlib
path = '/home/user/Desktop/test'
pattern = '*on'
all_entries = [entry.name for entry in pathlib.Path(path).glob(pattern)]
print(all_entries)
Output:
['python']
Frequently Asked Questions
-
How do I get list of all files in a directory?
Python has a rich library set to work with Files and Directories. We have used 3 libraries to list down all the files in a directory and recursively list all the files and sub-directories in a directory. Refer to this for code examples.
-
How do I list files in a directory and its subdirectories?
glob() function and walk() function from the os module supports listing all the files and folders recursively in Python. Listed down both methods with concrete code examples.
We have listed down different ways to get the list of all files in a directory using Python. For more insightful posts, follow CodeThreads.