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.

Recursively Matching

00:00 The main limitation that you’ve seen both with .iterdir() before and then also with .glob() is that it only matches in the directory that you’re directly in and none of the subdirectories. Now there’s another wildcard, the ** wildcard that allows you to do recursive matching, which means that it will apply the pattern both in the current directory as well as in any of the subdirectories.

00:24 So to make this a little more practical, take another look at the folder structure in here in the notes/ directory—that was the main directory that we did any sort of searching in—you have these two goals1.txt and goals2.txt.

00:37 And when you apply the search pattern of just searching for "*.txt", you would only match these two files because they’re directly nested inside of the notes/ folder.

00:48 But what about all the other .txt files that you have somewhere inside of this folder structure? Now you can match them as well, and you can do this using this recursive ** pattern.

01:03 So in IDLE, I’m going to say notes_dir.glob() and now use the ** pattern (**/), and then I’m going to say "*.txt".

01:16 The single * is the wildcard character you’ve seen before, but now we’re adding ** and /, which says to look also in all of the subdirectories, not just in the notes/ directory.

01:29 Let’s also put this inside of the list() function again to see the output. And as you can see, now you get, again, goals2.txt and goals1.txt that are directly inside of the notes/ directory.

01:42 But now you also get any .txt files that are nested in subdirectories of notes/, so here you have plans/goals3.txt, plans/monthly/february.txt, etc.

01:54 So there’s all of the .txt files that are anywhere inside of the folder structure of the notes/ directory. And you can do the same for Markdown files, for example. So I can say notes_dir.glob(), and I want to get all the Markdown files, let’s say. "/*.md".

02:16 And this is going to give me both Markdown files that are somewhere in this folder structure. The first one is directly in the notes/ directory—notes/README.mdand the other one is nested deeper inside of plans/monthly, and then it’s called march.md.

02:31 So this recursive pattern matching is quite powerful. There’s also a shortcut if you don’t want to type out this **/ all the time. There’s another method that is called .rglob().

02:43 So you can say notes_dir.rglob(), and that stands for using .glob() recursively. And then you can just, again, use the patterns that you’ve worked with before.

02:53 So I can say "*.md" to get all of the

02:58 Markdown files inside of the folder structure. So these two do the same. You can either use the **/ or you can use the recursive glob, the .rglob() method, and then just pass in the normal pattern.

03:16 To recap, recursive matching is very powerful. You can do it using the ** wildcard, and it matches the pattern both in the current directory as well as any of its subdirectories.

03:26 Now you’ve seen it with the .txt files and also with the Markdown files, and you use it by putting **/ at the beginning of your pattern, and then whatever else you want to match for the filename.

03:40 You can do recursive matching either using the ** wildcard, or you can also use the .rglob() method that is equivalent to using **/ inside of your pattern, but just a quicker way of doing the same thing.

03:53 And in this short code example, you saw that using .rglob(), you can achieve the same thing as using .glob() and the ** method.

04:02 And this wraps up the lessons about searching for files and folders inside of directories using wildcard characters and patterns. Now in the next lesson, you will learn about moving and deleting files and folders.

Become a Member to join the conversation.