Loading video player…

Getting a List of All Files and Folders in a Directory

00:00 In this lesson, you’ll learn how to get a list of all files and folders in a directory in Python. Getting a list of all files and folders in a directory is a natural first step to many file-related operations in Python. 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.

00:21 Most likely every solution will have its own advantages and trade-offs. In this course, you’ll be focusing on the most general purpose techniques in the pathlib module to list items in a directory.

00:34 It all begins with creating a Path object, which will be different depending on your operating system. On Windows, you’ll get a WindowsPath object while Linux and macOS will return PosixPath.

00:47 With that out of the way, it’s now time to dive into listing folder contents.

00:52 You can start by importing pathlib.

00:57 Next, set desktop equal to pathlib.Path with a capital P parentheses, and inside those parentheses, double quotes, and inside those quotes, you can add your path.

01:10 The path for this example is C:/Users/ Alexa/OneDrive/ Desktop.

01:23 You may notice this path includes backslashes. If your path looks similar, it may cause an issue. If you hit Enter, you can see the error.

01:32 This is because in Python, backslashes are commonly used as escape characters. To make it a raw string instead, you can add an r before your path.

01:42 This special prefix tells Python to treat backslashes as literal characters. So you can type desktop = pathlib.Path(

01:53 and this time add an r, and then your quotes with your path in between them. C:/Users/Alexa/ OneDrive/Desktop, and then close off those parentheses.

02:08 Now when you hit Enter, no error. Next, you can call the iterdir() method on your desktop path object. If you only need to list the contents of a given directory and you don’t need to get the contents of each subdirectory too, then you can use the Path object’s iterdir() method.

02:28 The iterdir() method, when called on a Path object, returns a generator that yields Path objects representing child items.

02:36 A generator in Python is a type of iterable object that generates values lazily. This will allow you to iterate over the contents of a directory without loading all file paths into memory at once, which can be useful when dealing with directories containing a large number of files.

02:52 You can jump over to your REPL. Next, you can call the iterdir() method on your desktop variable. The desktop variable contains a Path object representing the file path to your desktop directory.

03:05 When calling the iterdir() method on your desktop variable, it’ll return a generator that yields Path objects representing child items such as file or directory.

03:15 So you can type desktop.iterdir()

03:22 and to see your list of files and folders, you can wrap the generator in a list constructor. So you can type list(desktop.iterdir()),

03:33 and then your closing list parentheses. Hit Enter. As you can see, you get back a list of items. Some of the items you might see is desktop.ini, Notes, realpython, scripts, and `todo.txt. Passing the generator produced by iterdir() to the list constructor provides you with a list of Path objects representing all the items in the desktop directory.

04:00 The list of items contain both files and folders. In this demonstration, you’ll see WindowsPath objects. If you’re a Unix user, you’ll have PosixPath objects instead.

04:11 In the next lesson, you’ll explore getting only files with loops and conditionals.

Become a Member to join the conversation.