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.