Categories
Python Answers

How to use glob() to find files recursively with Python?

Spread the love

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.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *