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

Listing Recursively With .rglob()

00:00 In this lesson, you’ll learn about recursively listing with .rglob().

00:05 To understand recursive listing, it is a good idea to first familiarize yourself with the structure of directories. Directories are often compared with trees because of their recursive nature. In trees, the main trunk splits into various main branches.

00:19 Each main branch splits off into sub-branches, and sub-branches can continue to split off into further sub-branches on and on. If you take a look at the desktop directory, you can see the main trunk is Desktop.

00:35 To make this lesson more interesting, I added a bunch of files and folders to my desktop and its sub-directories. Desktop splits off into the main branches: Notes, realpython, scripts, and todo. Some of those main branches split off further.

00:50 Notes contains the sub-branch hash-tables. realpython contains two sub-branches: with iterate_dict and tictactoe. Scripts has two sub-branches as well, with rename_files and requests, and then todo doesn’t split any further.

01:06 Now that you familiarized yourself a bit with the directory structure, you can see how to recursively list with .rglob(). .rglob() is a method provided by the Path object in the pathlib.Path module.

01:17 .rglob() recursively searches for files that match a specified pattern in the current directory and all sub-directories. Using .rglob(), you can return absolutely everything.

01:29 You can try this out now.

01:32 Make sure you have import=pathlib at the top and assign the desktop variable to your desired path. To get started with the .rglob() method, start by typing desktop

01:44 .rglob() with parentheses. The .rglob() method expects at least one argument to match against. In this case, you want to recursively search for all files and directories within the desktop directory.

01:57 To do this, you can add the pattern of a single asterisk between two double quotes within the parentheses of .rglob(). So make sure you’re inside .rglob() and to pass in the argument you do two quotes and inside of those quotes a single asterisk.

02:13 This pattern signifies that all files and directories should be returned. If you hit Enter, this will return a generator object yielding path objects corresponding to the files and directories found.

02:25 You may recall from the previous lesson, you can wrap a list constructor around the generator to materialize the items. So type list(desktop.rglob('*')), and then within the parentheses double quotes, and inside those quotes, the single asterisk and then close that off with the remaining parenthesis.

02:49 Now hit Enter and you can see all the files and folders were returned recursively. For example, not only will you get back the main branches such as Notes, realpython and scripts, but you get each of their sub-branches now too, such as hash-tables, iterate-dict, and tictactoe.

03:07 You might be wondering more about the asterisk argument and what other patterns may be available to you. In the next lesson, you look into glob patterns and see how you can do more than just list all items in a directory.

Become a Member to join the conversation.