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 the default subtitles language 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 see our video player troubleshooting guide to resolve the issue.

Using the Square Bracket Wildcard

00:00 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 [].

00:15 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.

00:25 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 1 and 2 that you have in there, I’m going to say, give me either goals 1 or 3, but we don’t have a goals3 in there, so this is only going to match the goals1 file.

00:54 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.

01:08 You can consult the folder structure for it. As you can see inside of the notes/ directory, there is only goals1 and goals2, and the pattern says it should either say goals1 or goals3, which means that only goals1.txt is going to match with this pattern.

01:26 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 1 or 3 .txt, which in your case only matches goals1.txt.

01:41 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.