Python 3, an Illustrated Tour Transcripts
Chapter: The standard library
Lecture: pathlib

Login or purchase this course to watch this video and the rest of the course contents.
0:00 In this video we're going to talk about pathlib. This came about through peps 428 and 519.
0:06 So 428 adds pathlib to Python 3, this came about in Python 3.4. 519 adds protocols for paths so that standard libraries
0:16 that support operations on paths can use pathlib. Pathlib is included in the standard library
0:23 so you can create a path by importing the path class from pathlib. Here I'm making a path called env in my temp directory
0:32 and if this path exists on my file, and in my case it does, I can say list env.iterdir and that gets me back at generator
0:42 and if I materialize that into a list, it gives me back a sequence of all these posix paths which have everything that is in that directory.
0:52 Now posix path, because I'm running this on my Mac, that's what I get back. If I were to run this on a Windows machine,
0:58 I would get back a Windows path if temp env existed on a Windows machine. One of the nice things about this path instance
1:05 or these path classes that we can create is that we can do various manipulations on them. So it has overloaded the slash operator to do concatenation.
1:15 So if I want to make a file called missing inside of this env directory, I can say m=env / missing and that concatenates missing on to the end
1:25 and it gives me back a posix path that has missing on the end. Note that / will work on Windows systems as well
1:31 and rather than giving back a posix path if this temp env existed on my machine, it would give me back a Windows path.
1:39 I can call .exist and that says does this path exist, and it says false, missing doesn't exist. Also on my path, I can say open
1:49 and I can pass in if I want to write to it or whatnot. I can put that in context manager that will give me a file handle and I can write to that file.
1:56 I can do things like say parts and that will give me back a tuple of the various parts of the path,
2:02 note that it puts this leading / in here for the root, and we can say open again to read from it and read from it.
2:09 And then if we want to delete from it, we can say unlink. The nice thing about these is these are cross-platform.
2:15 These will work on Windows or posix systems, so Linux or Mac or other Unix systems. Here are some more examples of some manipulation that we can do,
2:25 note that in here I'm saying m.parent, that gives me the parent path of my directory
2:31 and then I'm concatenating on bin and concatenating on activate,
2:35 and on that py path I can say what's the root there, and it says the root is that, what's the drive there, and it says the drive is that,
2:43 what's the anchor, the anchor is this combination of root and drive, so drive here, this is Windows specific,
2:50 and if you're dealing with the c drive or the d drive or whatnot, that will pop up in there. The anchor here is the combination of root and drive.
3:00 A couple of other things you can do so I can say what's the parent path and the parent path of my py guy, which is the activate file is tmp/env/bin.
3:09 I can say what's my name and it will give me just the name of my file, I can say what's my suffix, my suffix is .py,
3:16 I can give it this stem attribute, that's the file name without the suffix, I can say is this an absolute path, it says true.
3:25 I can do matching against it with simple globbing operators, so does this match *.py, yes, it does match *.py.
3:32 Pathlib makes a distinction between what it calls pure paths and concrete paths. We mostly deal with concrete paths.
3:39 These are paths that have access to the file system, but you can also make Windows or posix pure paths on either operating system for manipulation.
3:47 So if I'm on a server that's a Linux server, but I need to manipulate Windows paths,
3:53 I can create a Windows path and manipulate it and get the drive and whatnot
3:58 and it will treat that as a Windows path and do the correct thing for me there. If we have concrete paths, we can do system calls on that.
4:05 Here I'm on my py path, I can say cwd, what's the current working directory, note that this is a method on this class,
4:13 but I can also call a class method on path because current working directory isn't dependent on a path,
4:20 it's dependent on your current process or where you are, in this case, I'm running this from the temp directory.
4:26 I can ask what the home is on any path, or I can call it directly on the class, in this case, my home is users/matt.
4:33 I can say give me the stat, this gives me the modification times of my file here. I can also do things like say expand user with the tilde
4:43 and that gives me where my home directory is, we can do globbing operators here, so I can say I want to get the home path and I want to glob onto that
4:54 anything that ends in py and I can get all the py files in my home directory. This is commented out because there's a lot of output here,
5:01 but I can also use a glob operator with **, if you use a ** that does a recursive search.
5:08 So this is going to search through everything in my home directory that's a Python file.
5:11 So nice functionality there to allow you to do various operations on a path. So in this video we talked about paths,
5:19 this is a nice feature that's been added in Python 3. This has combined a lot of the functionality in the OS modul,
5:26 a lot of people have been using third-party path libraries, and so this is a welcomed feature into Python 3.
5:32 Hopefully you can make use of this if you're dealing with files and directories.

Talk Python's Mastodon Michael Kennedy's Mastodon