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!
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.
00:00
Well done! You’ve made it to the end of the course. You now have an understanding of how to get a list of all files in a directory with Python using the pathlib
module.
00:10
In this course, you learned: how to use the iterdir()
method to get a list of all files and folders, made use of loops and conditionals to get only files, learned how to recursively list with .rglob()
, explored Python glob
patterns, conditionally listed using the glob
methods, and learned how to perform advanced matching with the glob
methods.
00:33 You may be wondering where to go from here. Here are two courses and tutorials that you can explore now. The first one is the tutorial on how to get a list of all files in a directory with Python.
00:44
You can also learn more about Python’s pathlib
module in Python’s pathlib
Module: Taming the File System, which includes both a tutorial and video course.
00:53 Congratulations on making it to the end of the course, and I hope to see you again soon on realpython.com.
Become a Member to join the conversation.
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.