Listing Conditionally Using .glob()
00:00
In this lesson, you learn conditional listing using .glob()
.
00:05
The .glob()
method of a path object behaves in much the same way as the .rglob
method, but does not perform recursion by default.
00:14
When you pass the asterisk argument into the .glob()
method, you retrieve a list of items in the directory without recursion. You can try this now.
00:23
First, make sure you still have import pathlib
at the top and assign the desktop variable to your desired path. Now, type desktop.glob
, and then within the parentheses pass in your argument.
00:37 So quotes and a single asterisk inside the double quotes. Hitting Enter will display the generator returned.
00:45
You may recall from previous lesson, you can use a list constructor here to materialize the items. Type list(desktop.glob(
,
00:56 and inside those parentheses your double quotes and inside the quotes asterisk and then close off the list constructor with the remaining parenthesis and hit Enter.
01:08
Returned back to you is all the items in the directory that is represented by the path object without going into the sub-directories. You may notice in this way, it produces similar results as the .iterdir()
method.
01:21
This means you can use the resulting generator in a for
loop or comprehension just like you would for .iterdir()
. However, what really sets the glob methods apart are the different patterns that you can use to match only certain paths.
01:34
For example, if you wanted only paths that ended with .txt, then you can type the following: list and inside list you want to pass desktop.glob(
and inside of that double quotes and then *.txt
, and make sure to close off your list constructor with the ending parenthesis.
01:58
Now, if you hit Enter, you might see a list with only one item. This is because this directory in particular only has one text file, which is todo.txt
.
02:08
One of the patterns you previously learned was a single character followed by an asterisk, which will return every item that starts with that character. Try this out now. Type list(desktop.glob(
and then inside those parentheses add double quotes, and then a capital N
, followed by a single asterisk.
02:32 And make sure to close off your list constructor with the last parenthesis.
02:37
This would be a good time to remind you that on a Windows machine matches are case insensitive, so it’ll turn everything that starts with the character N
, regardless if it’s upper or lower case.
02:49
So if you hit Enter in this directory, you’ll get back Notes
. Notice it has an uppercase N, but if you type it again and use a lowercase n
and a single asterisk and then close off your list constructor, now hit Enter.
03:08 The result is the same, however, some systems may be case-sensitive. For example, if you’re on a Unix-like system, such as Linux or macOs, glob patterns are case-sensitive.
03:20 This means you may have seen two different results, depending if you used an uppercase or lowercase character.
03:26
What if you only wanted to get items that start with real
? Then you can use the following glob
pattern: list(
desktop.glob(
03:37
and inside those parentheses, double quotes and type real
followed by a single asterisk. And make sure to close off your list constructor. Now, hit Enter. This only produced one item One item starts with real
, which is realpython
.
03:54
You can also get the contents of a subdirectory by including the subdirectory name a slash and an asterisk. Try this out now. list(desktop.glob(
double quotes, and type realpython/*
.
04:16
And then make sure to close off your list constructor. Now hit Enter. In this example, you’ll see iterate
-dict.md
and tictactoe.md
because using the realpython/*
pattern yields all the files within the realpython directory.
04:34
Next up, you’ll look a bit further into filtering with .rglob()
and learn how it differs from the .glob()
method.
Become a Member to join the conversation.