Python 3, an Illustrated Tour Transcripts
Chapter: The standard library
Lecture: Walk-through: 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 look at path test, open that up in your editor.
0:03 Let's run it and make sure that our environment is configured.
0:09 Okay, we have one error, we're not getting failure with importing pytest,
0:13 so we're good there and we have one test function.
0:16 Let's go through this and figure out what's going on.
0:19 The first part, get the contents of the current directory using the path module
0:23 store the results in cur, the variable called cur.
0:27 So in Python 3, there is now a library called pathlib,
0:34 and we can say from pathlib import path
0:39 and let's get the current module, current equals path
0:45 and period should be the current guy.
0:50 If you want the contents of that you say iterdir,
0:54 and that will give you what is in the directory.
0:57 Let's run this and see if it works.
1:04 Okay, so we're only getting one failure which is fine
1:08 because we have one test and we're getting an error on line 16.
1:12 So it looks like the first guy worked.
1:15 So what iterdir does, given a path it gives you a sequence
1:18 that has all of the results in it or all of the files and directories that are in that.
1:24 So our test is just saying is path test in the current directory,
1:29 and because we're in that directory, it is. Cool.
1:32 Make a path with a file named test.txt, and store it in test file.
1:39 So test file equals path, and we're going to say test.txt, that should work.
1:48 I'll just run it and see if it works.
1:51 Okay, we're now on line 23, so we're down here.
1:54 Write hello world to test file,
1:57 we can use this path guy as we would a result of an open in the context manager
2:05 so I can say with test file and then I can call on that open
2:12 and I can pass in a mode,
2:15 so I'm going to pass in the mode write
2:17 and I'm going to say as f out: and I'll say f out.write, and we'll write hello world.
2:24 At this point, after we've exited from this, test file should exist,
2:28 it should have a name and we should assert that hello world is in there.
2:34 Let's run it and make sure it works.
2:36 Okay, we're now down to line 32, so we're at this last part.
2:41 Delete the test file.
2:43 So we have test file, how do we delete it? There is no delete.
2:46 Is there a remove? There is no remove.
2:51 Is there an rm? There's an rm dir.
2:53 So none of those are the way to delete a file,
2:56 the correct pronunciation of this is unlink.
2:59 Let's run this and see if it works.
3:02 Cool, we're good.
3:04 So hopefully, this gives you a brief chance to play around with path.
3:08 You can get the contents of a directory by saying iterdir,
3:11 you can create a path, you can write to it by putting in a context manager
3:16 and calling the open method on it, rather than using the built-in open function
3:21 and you can delete it if you want to as well.