Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Trying Out pathlib in Action

For a deeper dive into .iterdir() and the glob methods, read How to Get a List of All Files in a Directory With Python.

If you’d like to learn more about the walrus operator, then check out Real Python’s course or tutorial on the subject.

00:00 pathlib in Action. In this section, you’ll see some examples of how to use pathlib to deal with simple challenges. There are a few different ways to list many files.

00:11 The simplest is the .iterdir() method, which iterates over all files in the given directory. The example you’ll see on-screen combines .iterdir() with the collections.Counter class to count how many files there are of each file type in the current directory.

00:39 More flexible file listings can be created with the methods .glob() and .rglob(). The variation of code you see on-screen only counts file types starting with p.

01:00 This next example defines a function tree that will print a visual tree representing the file system hierarchy, rooted at a given directory. Here, we want to list subdirectories as well, so we use the .rglob() method.

01:32 Note that we need to know how far away from the root directory a file is located. To do this, we first use .relative_to() to represent a path relative to the root directory.

01:42 Then we count the number of directories. using the .parts property. in the representation. When run, this function creates a visual tree as seen on-screen.

02:06 The .iterdir(), .glob(), and .rglob() methods are great fits for generator expressions and list comprehensions. To find the file in a directory that was last modified, you can use the .stat() method to get information about the underlying files.

02:21 For instance, .stat().st_mtime gives the last modification of a file.

02:55 You can even get the contents of the file that was last modified with a similar expression.

03:10 The timestamp returned from the different .stat().st_ properties represents seconds since January 1, 1970, the Unix epoch time.

03:30 In addition to datetime.fromtimestamp, time.localtime or time.ctime may be used to convert the timestamps to something more human-readable.

03:48 This final example will show how to construct a unique numbered filename based on a template. The function will accept a directory and a pattern to match with.

04:01 The counter is started at 1, and a while statement is used, making use of the := (walrus) operator in this statement to check for whether the filename exists when combined with the current value of counter. If it does, then the counter is incremented, and the loop repeats. If not, then the path is returned, incorporating the first value of counter that didn’t match an existing file.

04:24 If you look at the top of the screen, you’ll see the directory already contains the text files test001 and test002. The unique_path() function is run with the current working directory and a name pattern, which will match with text files with three digits in them.

04:40 The return value of path is the first unique text file matching this pattern, text003. If you’d like to learn more about the := operator, take a look at this Real Python course.

04:57 In the next section of the course, you’ll take a look back at what you’ve learned.

Become a Member to join the conversation.