Listing All Files in a Directory With Python (Overview)
Getting a list of all the files and folders in a directory is a natural first step for many file-related operations in Python. When looking into it, though, you may be surprised to find various ways to go about it.
When you’re faced with many ways of doing something, it can be a good indication that there’s no one-size-fits-all solution to your problems. Most likely, every solution will have its own advantages and trade-offs. This is the case when it comes to getting a list of the contents of a directory in Python.
In this video course, you’ll be focusing on the most general-purpose techniques in the pathlib
module to list items in a directory, but you’ll also learn a bit about some alternative tools.
00:00 Welcome to the How to Get a List of All Files in a Directory with Python course. My name is Alexandra, and I’ll be your guide.
00:08
In this course, you’ll learn how to get a list of all files and folders in a directory in Python, how to get only files with loops and conditionals, how to recursively list with .rglob()
, how to use Python glob patterns, conditional listing using .glob()
, conditional listing using r.glob()
, and advanced matching with the glob methods.
00:30 If any of those methods sound strange to you, they’ll all be explained to you later throughout the course.
00:37 Before diving in, a couple quick notes of the tools used in this course. The examples will be demonstrated using the IPython REPL. However, feel free to use your favorite IDE, editor, or REPL.
00:49 This course will specifically be using a Windows path. If you’re on another operating system such as Linux or macOS, your path may look a little different.
00:58 For example, Windows typically includes the C Drive and where Unix-like systems typically represent this with a single forward slash.
01:08 In Windows systems, you’ll be alerted to those differences.
Become a Member to join the conversation.