Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Listing All Files in a Directory With Python (Summary)

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!

Download

Course Slides (.pdf)

691.8 KB
Download

Sample Code (.zip)

2.8 KB
Avatar image for mikesult

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.

Become a Member to join the conversation.