#6
Inspired Python
>>> for path in Path('/opt/documents/').rglob('*.doc'):
        print(path)
/opt/documents/tips/tip1.doc
/opt/documents/tips/tip2.doc
/opt/documents/tips/tip3.doc
/opt/documents/articles/common-pathlib-patterns.doc
/opt/documents/news/whats-new-python-3-10.doc
#  ... etc ...

Python Pathlib Tips: Recursively listing files and directories


Recursively listing files and directories – with wildcards – is trivial with the pathlib Path class and much easier than using scandir() in the os module. The rglob method returns a generator so you can consume a single match at a time.

Be Inspired Get Python tips sent to your inbox

We'll tell you about the latest courses and articles.

Absolutely no spam. We promise!