Searching for Files Using Glob Methods
In this lesson, you’ll see how you can search for specific files in a directory. Say you don’t want to iterate over all the contents of a directory like you did before with using
.iterdir(). Then you can use a method called
So, you want to only search for specific files or folders, and you search for them according to a specific pattern. So I can use a wildcard character, and we’ll start off with using the
and I need to pass this pattern as a string—
.glob() is going to filter the contents of the directory for only files that end with
.md, so only the ones that have the file extension
.md are going to be shown. Now, if you remember the lesson before, then there should be one of those files in there, and the two folders that are also contained inside of the
notes_dir should be filtered out. So if you now say
But to recap this first view on
.glob(), it returns an iterable of
Path objects that match a pattern. And then you got to know the
* character, which is a wildcard character that matches any number of other characters.
Become a Member to join the conversation.