Python 3.4+pathlibfunction
.rglob()
search
Match files against a pattern recursively in all subdirectories — equivalent to `.glob()` with a leading `**/`.
Syntax
p.rglob('*.txt')
It yields everything it matches, directories included, not just files — filter with .is_file() when you want files only. The walk covers the entire tree, .git, .venv and node_modules along with it, so on a big directory it is noticeably slow; the result is lazy, so nothing happens until you start iterating.
Examples
from pathlib import Path
p = Path('rglob_demo') # свой каталог, а не /tmp целиком: rglob обойдёт ВСЁ дерево
p.mkdir(exist_ok=True)
(p / 'conf').mkdir(exist_ok=True); (p / 'conf' / 'app.cfg').touch()
[x.name for x in p.rglob('*.cfg')] # → ['app.cfg'] — все .cfg рекурсивно
sorted(x.name for x in p.rglob('*')) # → ['app.cfg', 'conf'] — всё содержимое дерева
See also
Open in the full glossarydocs.python.orgReport an inaccuracy