glob
is useful if you are doing this in within python, however, your shell may not be passing in the *
(I’m not familiar with the windows shell).
For example, when I do the following:
import sys
print sys.argv
On my shell, I type:
$ python test.py *.jpg
I get this:
['test.py', 'test.jpg', 'wasp.jpg']
Notice that argv
does not contain "*.jpg"
The important lesson here is that most shells will expand the asterisk at the shell, before it is passed to your application.
In this case, to get the list of files, I would just do sys.argv[1:]
. Alternatively, you could escape the *
, so that python sees the literal *
. Then, you can use the glob
module.
$ getFileNames.py "*.jpg"
or
$ getFileNames.py *.jpg
The glob module, part of the Python Standard Library, is used to find the files and folders whose names follow a specific pattern. The searching rules are similar to the Unix Shell path expansion rules.
After reading this article, you will learn:
- How to find all files that match the specified pattern
- How to search files recursively using the
glob()
function - The
iglob()
to iterate over a list of filenames. - Search Files Using Wildcard Characters
The following are the list of functions available in the glob module. we’ll learn each one by one.
Function | Description |
---|---|
glob.glob(pathname) |
Returns a list of files that matches the path specified in the function argument |
glob.iglob(pathname) |
Return a generator object that we can iterate over and get the individual file names |
glob.escape(pathname) |
Useful especially in the case of the filenames with special characters |
Table of contents
- Python glob() Method to Search Files
- glob() to Search Files Recursively
- Glob to Search Files Using Wildcard Characters
- Match Any Character in File Name Using asterisk (*):
- Search all files and folders in given directory
- Match Single character in File Name Using Question Mark(?):
- Match File Name using a Range of Characters
- iglob() for Looping through the Files
- Search for Filenames with Special Characters using escape() method
- glob() Files with Multiple Extensions
- Using glob() with regex
- glob for finding text in files
- Sorting the glob() output
- Deleting files using glob()
- scandir() vs glob()
Python glob() Method to Search Files
Using the glob module we can search for exact file names or even specify part of it using the patterns created using wildcard characters.
These patterns are similar to regular expressions but much simpler.
- Asterisk (
*
): Matches zero or more characters - Question Mark (
?
) matches exactly one character - We can specify a range of alphanumeric characters inside the
[]
.
We need to import Python’s built-in glob module to use the glob()
function.
Syntax of glob()
function
glob.glob(pathname, *, recursive=False)
Python glob.glob()
method returns a list of files or folders that matches the path specified in the pathname
argument. This function takes two arguments, namely pathname, and recursive flag.
pathname
: Absolute (with full path and the file name) or relative (with UNIX shell-style wildcards). We can perform file search by passing the absolute or relative path to the glob() method.
An absolute path is a path name with a complete directory structure. A relative path is a pathname with one or more wild card characters in the path along with the directory names.recursive
: If set toTrue
it will search files recursively.
Example: Search all .txt files present in the current working directory
Let’s assume the following test files are present in the current working directory.
sales_march.txt profit_march.txt sales_april.txt profit_april.txt
import glob
# relative path to search all text files
files = glob.glob("*.txt")
print(files)
Output:
['profit_april.txt', 'profit_march.txt', 'sales_april.txt', 'sales_march.txt']
Example 2: Search files using a absolute path
Also, you can use the absolute path to search files.
import glob
# absolute path to search all text files inside a specific folder
path = r'E:/performance/pynative/*.txt'
print(glob.glob(path))
glob() to Search Files Recursively
Set recursive=True
to search inside all subdirectories. It is helpful If we are not sure exactly in which folder our search term or file is located. it recursively searches files under all subdirectories of the current directory.
The default value of the recursive flag is False
. I.e., it will only search in the folder specified in our search path. For example, if our search path is '/sales/abc.jpeg'
and you set recursive
to True
, it will search abc.jpeg
under all subfolders of sales.
Use Python 3.5+ to find files recursively using the glob module. The glob module supports the **
directive. When you set a recursive flag to True, the glob method parses the given path look recursively in the directories.
Example to search .txt files under all subdirectories of the current directory.
import glob
# path to search file
path = '**/*.txt'
for file in glob.glob(path, recursive=True):
print(file)
Output:
profit_april.txt profit_march.txt sales_april.txt sales_march.txt salesmarch_profit_2020.txt salesmarch_sales_2020.txt
Note: If the pathname has **
, the method will search for the directories and sub-directories. In a large file structure, this operation will typically consume a lot of time.
Glob to Search Files Using Wildcard Characters
We can use glob()
with wildcard characters to search for a folder or file in a multi-level directory. Two wildcards are most commonly used for search operations. Let us see both of them with examples.
Wildcard | Matches | Example |
---|---|---|
* |
Matches everything | *.pdf matches all files with the pdf extension |
? |
Matches any single character | sales/??.jpeg matches all files with two characters long present in the sales folder |
[] |
Matches any character in the sequence. | [psr]* matches files starting with the letter p, s, or r. |
[!] |
Matches any character not in sequence | [!psr]* matches files not starting with the letter p, s, or r. |
Match Any Character in File Name Using asterisk (*):
This wildcard character(*) will return a list of files or folders with zero or more character matches. We can extend our search of the glob() function using the wild character up to multi-level directories.
The following example will return all the files with a .txt extension and further extending the search in the subdirectory levels.
Example:
import glob
# path to search all txt files
path = "sales/*.txt"
for file in glob.glob(path):
print(file)
Output:
salesmarch_profit_2020.txt salesmarch_sales_2020.txt
Search all files and folders in given directory
Here we will see following three scenarios:
- Match every pathname inside a current directory, i.e. We will print all folders and files present inside the current directory
- Match every files and folder inside a given directory
- Match every files and folder that starts with the word ‘march’
import glob
# using glob to match every pathname
print('Inside current directory')
for item in glob.glob("*"):
print(item)
# Match every files and folder from a given folder
print('Inside Sales folder')
for item in glob.glob("sales/*"):
print(item)
print('All files starts with word march')
for item in glob.glob("sales/march*"):
print(item)
Output:
Inside current directory sales glob_demo.py profit_april.txt profit_march.txt sales_april.txt sales_march.txt Inside Sales folder salesbar.jpeg saleschart.jpeg salesmarch_profit_2020.txt salesmarch_sales_2020.txt salesp.jpeg All files starts with word march salesmarch_profit_2020.txt salesmarch_sales_2020.txt
Match Single character in File Name Using Question Mark(?):
This wildcard(?
) will return a list of files or folders with exactly one character match. This is generally used to search for a list of filenames, almost similar names with only one or few characters unique.
The following example will return all the files with single character names.
import glob
# path to search single character filename
path = "sales/?.jpeg"
for file in glob.glob(path):
print(file)
# path to search three-character filename
path = "sales/???.jpeg"
for file in glob.glob(path):
print(file)
# search file that starts with word 'cha' followed by exact two-character
path = "sales/cha??.txt"
for file in glob.glob(path):
print(file)
Output:
salesp.jpeg salesbar.jpeg saleschart.txt
Match File Name using a Range of Characters
We can give a range of characters or numbers as the search string by enclosing them inside the square brackets ([]
).
We can have either alphabets or numbers in the search pattern. The following example will show how to use glob to match files with characters from a-t and a list of files with numerals 2 to 5 in theirs names.
import glob
print(glob.glob("sales/[a-f]*.txt"))
print(glob.glob("sales/[2-5].*"))
Output:
['salesbar.txt', 'saleschart.txt']
['sales2.txt']
iglob()
for Looping through the Files
The glob.iglob()
works exactly the same as the glob()
method except it returns an iterator yielding file names matching the pattern. This method returns an iterator object that we can iterate over and get the individual file names.
Syntax:
glob.iglob(pathname, *, recursive=False)
Return an iterator which yields the same values as glob()
without actually storing them all simultaneously.
Why use iglob()
:
In some scenarios, the number of file or folders to match is high, and you could risk filling up your memory by loading them all using glob()
. Instead of that using the iglob()
, you can get all matching filenames in the form of an iterator object, which will improve performance.
It means, iglob()
returns a callable object which will load results in memory when called. Please refer to this Stackoverflow answer to get to know the performance benefits of iterators.
We can loop through the folders and subfolders to get the list of files in two ways.
Example
import glob
# using iglob
for item in glob.iglob("*.txt"):
print(item)
# check type
print('glob()')
print(type(glob.glob("*.txt")))
print('iglob()')
print(type(glob.iglob("*.txt")))
Output:
profit_april.txt profit_march.txt sales_april.txt sales_march.txt glob() <class 'list'> iglob() <class 'generator'>
Search for Filenames with Special Characters using escape()
method
In addition to the character and numeric ranges, we have the escape()
method to enable the pattern inside the glob()
with special characters.
syntax:
glob.escape(pathname)
As the name of the function suggests, this method escapes the special characters in the pathname
passed in the argument. This function is useful to search filenames with special characters like _, #, $, etc.
We can use this method along with the glob()
while searching for filenames with special characters. Let us see an example to find the files with special characters in their names.
import glob
print("All JPEG's files")
print(glob.glob("*.jpeg"))
print("JPEGs files with special characters in their name")
# set of special characters _, $, #
char_seq = "_$#"
for char in char_seq:
esc_set = "*" + glob.escape(char) + "*" + ".jpeg"
for file in (glob.glob(esc_set)):
print(file)
Output
All JPEG's files ['abc.jpeg', 'y_.jpeg', 'z$.jpeg', 'x#.jpeg'] JPEGs files with special characters in their name y_.jpeg z$.jpeg x#.jpeg
glob() Files with Multiple Extensions
We can search files having different extensions using the glob module. For example, you wanted to find files having .pdf or .txt extensions from a given folder.
import glob
print("All pdf and txt files")
extensions = ('*.pdf', '*.jpeg')
files_list = []
for ext in extensions:
files_list.extend(glob.glob(ext))
print(files_list)
Output
['christmas_envelope.pdf', 'reindeer.pdf', '1.jpeg', '2.jpeg', '4.jpeg', '3.jpeg', 'abc.jpeg']
Using glob() with regex
The glob()
function internally calls the fnmatch.fnmatch
which uses only the following four rules for pattern matching.
If you want to extend file matching with more flexible rules, we can combine the glob with regular expressions.
Consider a folder with jpeg files for employees, and we want to search for an employee whose name matches the user input. We can mention the folder name where the glob has to search and then use the regex search to search pattern.
import glob
import re
num = input('Enter the employee number ')
# [a-z] for any employee name
# {file_name} is the employee number
regex = r'[a-z_]+{file_num}.*'.format(file_num=num)
# search emp jpeg in employees folder
for file in glob.glob("2020/*"):
if re.search(regex, file):
print('Employee Photo:', file)
Output:
Enter the employee number 3 Employee Photo: 2020emp_3.jpeg
glob for finding text in files
The glob module is also handy for finding text in files. We generally use the glob module to find matching file names.
But most of the time, we wanted to replace a specific word from a file. Or we wanted files that contain the exact text, such as user id.
We can follow the below steps to get the files that contain the specific text
- Use glob to list all files in a directory and its subdirectories that match a file search pattern.
- Next, read the file and search for the matching text. (You can use regex if you wanted to find a specific pattern in the file)
Example: Search word profit in files
import glob
# Look all txt files of current directory and its sub-directories
path = '**/*.txt'
search_word = 'profit'
# list to store files that contain matching word
final_files = []
for file in glob.glob(path, recursive=True):
try:
with open(file) as fp:
# read the file as a string
data = fp.read()
if search_word in data:
final_files.append(file)
except:
print('Exception while reading file')
print(final_files)
Output:
['salesdata_2021.txt']
Sorting the glob() output
We can sort the output files list of the glob() method simply by using the sorted() function.
import glob
path = "*.txt"
print(sorted(glob.glob(path)))
Output:
['profit_april.txt', 'profit_march.txt', 'sales_april.txt', 'sales_march.txt']
We can sort the files based on the date and time of modification by combining the glob()
method with the getmtime()
method in the os module.
import glob
import os
# List all files and folders in the current directory
files = glob.glob(os.path.expanduser("*"))
# Sort by modification time (mtime) ascending and descending
files_ascending = sorted(files, key=lambda t: os.stat(t).st_mtime)
print(files_ascending)
files_descending = sorted(files, key=lambda t: -os.stat(t).st_mtime)
print(files_descending)
Output:
['sales_april.txt', 'sales_march.txt', 'profit_april.txt', 'profit_march.txt', 'sales', 'glob_demo.py'] ['glob_demo.py', 'sales', 'profit_march.txt', 'profit_april.txt', 'sales_april.txt', 'sales_march.txt']
Deleting files using glob()
We can remove the files from the directories using the glob() method by iterating over the list and then calling the os.remove()
for that file.
import glob
import os
# delete all pdf files
for pdf in (glob.glob("2020/*.pdf")):
# Removing the pdf file from the directory
print("Removing ", pdf)
os.remove(pdf)
Output:
Removing salesjune.pdf
scandir() vs glob()
Both the scandir()
and glob()
functions are internally searching for the files in a directory that matches a particular pattern.
But scandir()
is a generator function that returns an iterator object. The glob() method instead returns a list that consumes a lot of memory.
What is the Python glob module? For which type of Python applications can you use it?
The Python glob module is used to find pathnames that match a particular pattern. The general term “glob” is used to describe methods for matching specific patterns according to Unix shell rules. With glob, you can also use wildcards (“*, ?, [ranges]) apart from exact string search to make path retrieval more powerful.
Let’s learn the Python glob module!
The Python glob module allows searching over pathnames to find files that fit a given pattern (which is defined by you). The rules established by the Unix shell are used to define the supplied pattern for file matching.
All file paths that match a certain pattern are returned using the glob function of the glob module. We may use glob to search for a certain file pattern, or perhaps more effectively, we can utilize wildcard characters to search for files where the filenames match a given pattern.
To use glob, we have to import it using the Python import statement as shown below.
import glob
Note: glob is a built-in module that comes with Python so you do not need to install it separately from your Python installation (e.g. using pip or conda) before importing it.
After importing the glob module we can use it to find the files that match a specific pattern on our local machine
The following code snippet matches a specific filename in the directory /opt/app/tutorial/ and shows it in the output.
import glob
file = glob.glob("/opt/app/tutorial/file1.txt")
print(file)
The output is:
['/opt/app/tutorial/file1.txt']
In the code above, we are searching for only one text file, we can also search for multiple files with the same file extension.
import glob
files = glob.glob("/opt/app/tutorial/*.txt")
print(files)
As you can see, we have specified “*.txt” which matches all the files with txt extension in the directory specified. The character “*” is a wildcard.
Let’s assume we have the following five txt files in the directory /opt/app/tutorial/:
file1.txt file2.txt file3.txt file4.txt file5.txt
After executing the Python code I get the following output:
['/opt/app/tutorial/file2.txt', '/opt/app/tutorial/file3.txt', '/opt/app/tutorial/file1.txt', '/opt/app/tutorial/file4.txt', '/opt/app/tutorial/file5.txt']
The glob.glob() function returns the list of all the files that match the path we have specified.
You can also see that…
The glob() function of the Python glob module returns results in arbitrary order.
Using Absolute and Relative Paths with Python glob
The path we provide to the glob() function can be absolute or relative.
Let’s see what this means…
- Absolute path: this is the full path of the file from the root of the filesystem.
- Relative path: this is the path relative to the current directory.
The two code snippets we have seen before use an absolute path.
Now, let’s see one example using a relative path…
import glob
files = glob.glob("*.txt")
print(files)
The path specified in this code snippet is a relative path and it refers to the current directory.
From the output you can see that the list of files only contains filenames and not the full path for each file:
['file2.txt', 'file3.txt', 'file1.txt', 'file4.txt', 'file5.txt']
How to Use glob to Find Files Recursively
The word recursive means repetition which relates to the repeated application of a rule. We can also use glob to search files recursively which means to search files in subdirectories.
The recursive nature of the glob module is useful because you might not know the exact location of the files you are looking for.
To enable the recursive behavior, you have to pass the recursive boolean argument to the glob() function set to True. This argument is False by default.
Before using the recursive argument we want to understand the behaviour of the double-asterisk (**) wildcard when used with the glob.glob() function.
Create a directory called test_dir in the current directory and then inside test_dir create a file called file6.txt. Our directory structure becomes the following:
file1.txt file2.txt file3.txt file4.txt file5.txt test_dir/file6.txt
Now execute the previous Python code that uses “*.txt” as pathname expression. The output is the following:
['file2.txt', 'file3.txt', 'file1.txt', 'file4.txt', 'file5.txt']
Now, pass the recursive argument to the glob function:
import glob
files = glob.glob("*.txt", recursive=True)
print(files)
When you execute the code you will notice that the output doesn’t change. That’s because the recursive argument has to be used in conjunction with the double-asterisk wildcard (**).
Update the expression passed to the glob() function as shown below (don’t change anything else in the previous code):
files = glob.glob("**/*.txt", recursive=True)
This time in the output you will also see “test_dir/file6.txt”. Our code has matched the .txt files in the current directory and also the .txt file in the test_dir subdirectory.
['file2.txt', 'file3.txt', 'file1.txt', 'file4.txt', 'file5.txt', 'test_dir/file6.txt']
Now, let’s try to set recursive to False without changing the pathname expression:
files = glob.glob("**/*.txt", recursive=False)
The output is:
['test_dir/file6.txt']
Our code has only matched the file in the subdirectory.
You now know how the double-asterisk works together with the recursive parameter when using the glob() function.
Using Wildcard Characters with glob
With the glob module you can use wildcards. There are many wildcard characters but the most used wildcard characters are explained below.
Asterisk (*): glob uses the asterisk (*) which is also known as the star to match zero or more characters.
filepath = "*.txt"
filepath = "*.py"
filepath = "*.jpg"
In the examples above, the * will match all the files with the specified extension.
Double Asterisk (**): two asterisks (**) are the same as the single asterisk but they work with subfolders. When you are searching recursively then use the double asterisk (**) to search for files in the current folders as well as in the subfolders.
filepath = "docs/**/*.txt"
filepath = "python/**/*.py"
filepath = "images/**/*.jpg"
Square Brackets ([ ]): square brackets are a very powerful wildcard character because they allow to search for files using different combinations of characters.
For example:
- Let’s say you want to search for files whose name matches lowercase vowels, to achieve this, you will specify all the lowercase vowels within square brackets: [aeiou].
- [0-9]: If you want to match any digits then specify digits from 0 to 9 within square brackets.
- [A-Z]: it matches any uppercase letters, [a-z] matches any lowercase letters. You can also combine uppercase and lowercase letters this way: [a-z,A-Z].
- If you want to exclude certain characters then you can use square brackets and also specify the (!) symbol. For example: [!abc].
Let’s try some practical examples to understand the wildcard characters.
We have already seen previously how the * and ** wildcards work. Now let’s see an example with the square brackets ([ ]).
Within square brackets, we specify the pattern of numbers or letters to search for:
import glob
path1 = "[a-e]*.txt"
print('Files with a name that starts with a letter between a and e in the current directory')
print(glob.glob(path1))
path2 = "[1-5]*.txt"
print('Files with a name that starts with a number between 1 and 5 in the current directory')
print(glob.glob(path2))
Create the following files in the current directory to confirm the wildcard expressions work as expected:
a.txt ask.txt cat.txt d.txt echo.txt delta.txt fox.txt 1.txt 134.txt 345.txt 67.txt
And now execute the code.
The output is:
Files with a name that starts with a letter between a and e in the current directory
['echo.txt', 'ask.txt', 'a.txt', 'd.txt', 'delta.txt', 'cat.txt']
Files with a name that starts with a number between 1 and 5 in the current directory
['345.txt', '1.txt', '134.txt']
Our code works as expected and the files “fox.txt” and “67.txt” are not matched.
Why Would You Use iglob vs glob in Python?
Until now we have used the glob() function. In the glob module there is another function called iglob().
The iglob() function is similar to the glob() function, the main difference is that iglob() returns a generator which yields file names matching the given pattern.
Let’s confirm the type of object the iglob() function returns by using one of the previous code examples. We will just replace the glob function with the iglob function.
import glob
files = glob.iglob("*.txt")
print(type(files))
[output]
<class 'generator'>
The glob() function goes through all the files and stores them in the memory at once, while iglob() returns a generator which allows to iterate through all the files without storing them simultaneously in the memory.
By passing the generator to the Python next() function you will get back the first filename returned by the iglob function:
print(next(files))
[output]
file2.txt
The iglob() function is very useful when we are matching a large number of files. We could risk filling up the whole memory by loading them all using glob().
To avoid this, the iglob() helps us match all the filenames in the form of a generator which improves performance and reduces memory usage.
To print all the files matched by the iglob function you can use a Python for loop:
import glob
files = glob.iglob("*.txt")
for filename in files:
print(filename)
Conclusion
You now should have a clear understanding of how the glob module works. You have learned how the glob module is particularly useful for tasks involving filename pattern matching and how to obtain a list of all the files that adhere to a particular pattern.
We covered some practical examples of how the glob module actually works, including some of the most used wildcard characters.
And you have also seen the difference between glob() and iglob() and why you should use iglob() over the glob() function.
Bonus read: deepen your understanding of Python and learn more about Python yield that we have mentioned when describing the behaviour of the iglob() function.
I’m a Software Engineer and Programming Coach. I want to help you in your journey to become a Super Developer!
Типовая задача – найти все файлы в каталоге с определенным расширением. Вторая разновидность задачи – найти все файлы с определенным расширением, включая подкаталоги
Решение:
-
Используя glob:
import glob, os
os.chdir(“/mydir”)
for file in glob.glob(“*.txt”):
print(file) -
Используя os.listdir:
import os
for file in os.listdir(“/mydir”):
if file.endswith(“.txt”):
print(os.path.join(“/mydir”, file)) -
Рекурсивный обход всех папок, с помощью os.walk:
import os
for root, dirs, files in os.walk(“/mydir”):
for file in files:
if file.endswith(“.txt”):
print(os.path.join(root, file))
Использованы примеры с http://stackoverflow.com/questions/3964681/find-all-files-in-a-directory-with-extension-txt-in-python
- Метод поиска файлов с определённым расширением
glob.glob
- Метод
os.listdir()
для поиска файлов с определённым расширением - Метод
pathlib.glob
для поиска файлов с определённым расширением - Поиск файлов с определенным расширением в каталоге и его подкаталогах на Python
- Модуль
pathlib
ищет файлы рекурсивно
В статье представлены различные методы поиска файлов с определённым расширением только на Python.
Метод поиска файлов с определённым расширением glob.glob
Мы могли бы использовать модуль glob.glob
для поиска файлов с определённым расширением только на Python.
import glob
targetPattern = r"C:Test*.txt"
glob.glob(targetPattern)
Приведенные выше коды демонстрируют, как найти файлы с расширением txt
в директории C:Test
.
Метод os.listdir()
для поиска файлов с определённым расширением
Функция os.listdir()
перечисляет все файлы в данной директории, без информации о пути к файлу. Вы можете извлечь файлы с определенным расширением, используя функцию str.endswith()
.
>>> import os
>>> fileDir = r"C:Test"
>>> fileExt = r".txt"
>>> [_ for _ in os.listdir(fileDir) if _.endswith(fileExt)]
['test.txt', 'test1.txt']
Вам необходимо построить полный путь с помощью функции os.path.join()
.
>>> import os
>>> fileDir = r"C:Test"
>>> fileExt = r".txt"
>>> [os.path.join(fileDir, _) for _ in os.listdir(fileDir) if _.endswith(fileExt)]
['C:\Test\test.txt', 'C:\Test\test1.txt']
Метод pathlib.glob
для поиска файлов с определённым расширением
pathlib
модуль представлен на Python 3.4, который предлагает объектно-ориентированные пути к файловой системе. Он предоставляет два стиля: Пути Windows в ОС Windows и пути POSIX в Unix-alike системах.
>>> import pathlib
>>> fileDir = r"C:Test"
>>> fileExt = r"*.txt"
>>> list(pathlib.Path(fileDir).glob(fileExt))
[WindowsPath('C:/Test/test.txt'), WindowsPath('C:/Test/test1.txt')]
Результат представлен с помощью WindowsPath
, и вы можете преобразовать результат в строковое представление, добавив str()
, типа
>>> [str(_) for _ in pathlib.Path(fileDir).glob(fileExt)]
['C:\Test\test.txt', 'C:\Test\test.txt']
Поиск файлов с определенным расширением в каталоге и его подкаталогах на Python
Шаблон C:Test*.txt
ищет только txt
файлы в каталоге C:Test
, но не в его подкаталогах. Если вы также хотите получить txt
файлы в подкаталогах, вы можете немного изменить шаблон.
import glob
targetPattern = r"C:Test***.txt"
glob.glob(targetPattern)
Подстановка **
между Test
и *.txt
означает, что он должен найти txt
файлы как в каталоге, так и в его подкаталогах.
Модуль pathlib
ищет файлы рекурсивно
Подобно добавлению **
в glob.glob
для рекурсивного поиска файлов, вы также можете добавить **
в pathlib.Path.glob
метод для рекурсивного поиска файлов с определённым расширением.
>>> import pathlib
>>> fileDir = r"C:Test"
>>> fileExt = r"***.txt"
>>> list(pathlib.Path(fileDir).glob(fileExt))
[WindowsPath('C:/Test/test.txt'), WindowsPath('C:/Test/test1.txt'), WindowsPath('C:/Test/sub/test1.txt')]