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