In this video course, you’ve explored the .glob()
, .rglob()
, and .iterdir()
methods from the Python pathlib
module to get all the files and folders in a given directory into a list. You’ve covered listing the files and folders that are direct descendants of the directory, and you’ve also looked at recursive listing.
In general, you’ve seen that if you just need a basic list of the items in the directory, without recursion, then .iterdir()
is the cleanest method to use, thanks to its descriptive name. It’s also more efficient at this job. If, however, you need a recursive list, then you’re best to go with .rglob()
, which will be faster than an equivalent recursive .iterdir()
.
You’ve also examined one example in which using .iterdir()
to list recursively can produce a huge performance benefit—when you have junk folders that you want to opt out of iterating over.
In the downloadable materials, you’ll find various implementations of methods to get a basic list of files from both the pathlib
and os
modules, along with a couple scripts that time them all against one another. Check them out, modify them, and share your findings in the comments!
Congratulations, you made it to the end of the course! What’s your #1 takeaway or favorite thing you learned? How are you going to put your newfound skills to use? Leave a comment in the discussion section and let us know.
mikesult on June 19, 2024
Thanks for the tutorial on the pathlib.Path object. Good to learn of the functionality of .iterdir() .glob() and .rglob(). I liked that you demonstrated 3 different ways to filter the results from .rglob() it broadened my understanding of list comprehensions and the filter() function.