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

Unlock This Lesson

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

Unlock This Lesson

Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set your subtitle preferences in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please refer to our video player troubleshooting guide for assistance.

Iterating Over Directory Contents

00:00 With pathlib, you can also iterate over the contents of a directory. The contents of a directory are always either a subdirectory or a file, and you can iterate over them in order to do some processing on all of the contents. This could be many things.

00:15 It could be some edits, or it could also be just listing them. Now, let’s start by just listing them out. I’ll iterate over this notes directory that you created earlier on.

00:26 And you can do that by using the .iterdir() method on a Path object that points to a directory. So let’s confirm that this points to a directory.

00:37 Since that is True, I can say notes_dir.iterdir(), and calling this method on a Path object that is a folder is going to return an iterable.

00:50 So you can see that here I get a generator object Path.iterdir at a certain memory location. This iterator means that I can iterate over it.

00:58 So I can write a for loop that goes over each of the contents of this directory. for path in notes_dir.iterdir():

01:11 print(path). And you can see that this contains two folders. One folder is called plans. The other one is called yearly.

01:22 And those are the two folders that you created in a previous lesson. Now, if this folder directly contained a file, then the file would be listed on here just as well.

01:32 Let’s confirm that by creating a new file object in here. I will make a README file … inside of notes_dir / "README.md" (Markdown), of course.

01:49 And then create the file.

01:55 And now if I do this iteration again for path in notes_dir.iterdir(): print(path)

02:07 you can see that it also lists the new file that I just created now. So using .iterdir() is a way that you can list or also process all the contents inside of a directory.

02:19 Now if you just want to see them and you don’t want to write a for loop, then you can pass the iterator to the list() function.

02:30 And here you also see that what .iterdir() returns are actually Path objects. So, as you can see, the iterator returns Path objects that represent all of the contents of the directory that you iterated over.

02:44 Usually you won’t have to pass the generator to the list() function, but more commonly, you will want to do something with all of the files or directories inside of a folder, so you will use the iterator inside of a loop and then apply some sort of of processing on it.

03:03 So, with Python’s pathlib module, you can iterate over directory contents using the .iterdir() method. And this returns an iterable of Path objects that are contained within that directory.

Become a Member to join the conversation.