Using the Square Bracket Wildcard
Let’s take a look at the
 (square bracket) wildcard. This wildcard matches, again, a single character, but unlike the
? character, it only matches specific single characters, specifically those that you put inside of the
So while the square
 that you see here in the heading wouldn’t match anything because there’s nothing in there, let’s look at a practical example where it actually matches something.
Back here in IDLE, in the notes directory, I’m going to say
notes_dir.glob(), and now the pattern that I’m building here is going to say
"goals.txt. But now, instead of the
? here that would match
2 that you have in there, I’m going to say, give me either goals
3, but we don’t have a
goals3 in there, so this is only going to match the
Let’s wrap it into the
list() call again so we see the output, and as you can see, it only matches
goals1, because there is no
goals3 file that is directly in the notes directory.
You can consult the folder structure for it. As you can see inside of the
notes/ directory, there is only
goals2, and the pattern says it should either say
goals3, which means that only
goals1.txt is going to match with this pattern.
So it matches a single character of those that are contained in the
. You saw the example of filtering for any file that is
goals and then a number, either
.txt, which in your case only matches
And just like the other wildcard characters, you can also use the
 wildcard multiple times in a single pattern, and you can combine it with the other wildcard characters to build pretty complex search patterns.
01:55 So while this is pretty cool and interesting, one limitation that you’ve seen with all of these wildcard characters so far is that they only search directly within a certain directory.
02:06 It did not step into subdirectories and search there as well. Now in the next lesson, you’ll learn about another wildcard character that allows you to do just that.
Become a Member to join the conversation.