Matching Advanced Patterns With the Glob Methods
00:00
In this lesson, you will learn advanced matching with the glob
methods. One of the potential drawbacks with the glob
methods is that you can only select files based on glob
patterns.
00:11 To do more advanced matching or filtering on the attributes of the items, you need to do something extra. To run more complex matching and filtering, you can follow at least three strategies.
00:22
You can use a for
loop with a conditional check, a comprehension with a conditional expression, or you could use the built-in filter()
function.
00:32
Head over to your code to try these strategies out now. You can start with the first strategy listed, which is using a for
loop. Type for item in
desktop.glob("*")
And inside those quotes, a single asterisk.
00:53
Remember from the previous lessons, the .rglob()
method with a single asterisk as an argument will recursively search for all files and folders from the path object.
01:03
Make sure to add the colon at the end of your line and then hit Enter to drop into the for
loop body. This is where you can add a conditional check.
01:11
You can check if the items returned are all files type. if item.is_file():
and hit Enter and type print parentheses, and inside those parentheses type print(item)
. When you hit Enter, you’ll see items that are files returned.
01:33
For this example, you might see todo.txt
, hash-tables.md
, iterate_dict.md
, tictactoe.md
, rename_files.py
, and request.py
.
01:48
The next strategy is to use a comprehension with a conditional expression. You can start by typing [item for item
in desktop.rglob("*")
02:03
and then double quotes inside those parentheses and a single asterisk. Then outside those parentheses type if item.is_file()]
and then parenthesis and close it off with another square bracket.
02:18 You may recall from the previous lesson, but what this is doing is filtering the resulting list by using a conditional expression inside the comprehension to check if the item is a file.
02:30
So when you hit Enter, you should see the same listed items that you saw when using the for
loop. For this example, that means a couple items returned were todo.txt
, hash_tables.md
, and iterate_dict.md
.
02:47
The last strategy you’ll try out is using the filter()
function. Once again, the goal will be to have the same items returned that we saw from the previous two strategies.
02:57
So try the strategy out now. Start by typing list(filter(
lambda item:
03:08 `item.is_file(), desktop.glob(“*”)))` with parentheses, your double quotes and the single asterisk. And then you need to close off two sets of parentheses.
03:28
Before hitting Enter, here’s a bit of what’s happening here. The lambda
function takes one argument, item
, and checks if it’s a file using the is_file()
method. This lambda
function is passed as the first argument to the filter()
function.
03:43
The filter()
function iterates over all the items yielded by the .rglob()
method and applies the lambda
function to each of them. It returns an iterator containing only the items for which the lambda
function returns True
.
03:55 For example, only files not directories.
03:59
Now hit Enter. And you’ll see the same items that you saw before using the two previous strategies such as todo.txt
, hash_tables.md
, iterate_dict.md
, and the rest.
04:13
So with these three strategies, you first called the .rglob()
method with the single asterisk pattern to get all items recursively. This produces all the items in the directory in its subdirectories.
04:26 Then using the different strategies to filter out the items that aren’t files you return all items that were files. With these strategies, you now know enough to be dangerous.
Become a Member to join the conversation.