Categories
Python Answers

How to do recursive folder read with Python?

Spread the love

Sometimes, we want to do recursive folder read with Python.

In this article, we’ll look at how to do recursive folder read with Python.

How to do recursive folder read with Python?

To do recursive folder read with Python, we can use the iglob method.

For instance, we write

import glob

for filename in glob.iglob(root_dir + '**/*.txt', recursive=True):
     print(filename)

to call glob.iglob with the pattern of the files we’re looking for and the recursive arguemnt set to True to traverse the files results.

We get an iterator from iglob and use a for loop to loop through the results.

And we print the filename returned in the loop.

Conclusion

To do recursive folder read with Python, we can use the iglob 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 *