Sometimes, we want to use glob() to find files recursively with Python.
In this article, we’ll look at how to use glob() to find files recursively with Python.
How to use glob() to find files recursively with Python?
To use glob() to find files recursively with Python, we can use the rglob
method.
For instance, we write
from pathlib import Path
for path in Path('src').rglob('*.c'):
print(path.name)
to call create the Path
object and then call rglob
to find any files with extension .c in the src
folder.
All child directories at all levels will be searched with rglob
.
Then the name
property has the path name of the file found.
Conclusion
To use glob() to find files recursively with Python, we can use the rglob
method.