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

Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set the default subtitles language in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please see our video player troubleshooting guide to resolve the issue.

Resolving Relative Paths

00:00 You might remember that the current working directory of IDLE is going to be Users/, your username, and then the Documents/ folder. So what .resolve() does in this case, it just sticks the output of this, Path.cwd(), together with whatever relative path you pass it. So while it works out in this case, you may have another relative path

00:28 that points to something else. Let’s say, for example, you’d include the Documents/ folder and then hello.txt.

00:40 Now if you would use .resolve() on this,

00:44 the output is probably not what you’d expect. You get a double Documents/ folder. So as you can see, it basically just takes your current working directory and then sticks whatever relative path you call the .resolve() method on at the end of it and bunches it together like this. Now, that’s not entirely true.

01:02 .resolve() does a little more than that, but in most situations this is what you’re going to experience. So it’s important that you’re aware of what is your current working directory and that you are working relative from your current working directory.

01:16 Now when I say that it does something else, what is that? I can give you a little preview. For example, if you just—let’s assign that to p = pathlib.Path().

01:29 If you create an empty Path object, then where does it point to? You can see that it gives you this slightly cryptic-looking . (dot).

01:38 And the . is a link to the current working directory. So if you would resolve this path,

01:46 then it actually gives you the current working directory. And that’s possible because .resolve() can handle links, like . or also .. and it does more than just always stick in the current working directory because if I would—let’s do another. relpath. Running out of variable names here. pathlib.Path().

02:10 I’m going to create a strange-looking path. So this using .. is a link for going one directory up in the hierarchy. So let’s do two of those "../../" and then

02:22 "hello.txt". So this is a valid path. relpath is a Path object, and you can see those links are part of the Path object. And now if I went to resolve this path,

02:39 then it will actually step two directories upwards from the current working directory, which was /Users/martin/Documents. You can see the first .. steps out of Documents.

02:49 The second .. steps out of martin and then concatenates the rest, hello.txt, to the end of it. So here you have an absolute path that says, start with the root directory, into users, and then assumes that there’s a hello.txt there.

03:04 So what .resolve() does is it attempts to create an absolute path from the piece that you give to it. And what you’ll mostly experience is that it sticks the current working directory at the beginning and then whatever relative path you pass after it.

03:21 But it can also resolve links such as a single dot, which stands for the current working directory, or a double dot, which says move one directory up.

03:33 Let’s recap this again. The two methods that you get to know about now is, at first, .is_absolute(), which returns True for absolute paths, otherwise False.

03:42 And that’s a good way to check whether the Path object that you’re working with is an absolute path or not. And then you also saw how to work with .resolve(), which creates an absolute path while also resolving links, such as . (dot), which stands for the current working directory, or .. (dot dot), which stands for one directory above the current working directory.

04:02 And .resolve() can also resolve custom-made symbolic links. But we didn’t look at that in this lesson. And if you really need this, I would suggest that you look it up in the documentation. .resolve() can be a helpful method sometimes to create absolute paths out of relative paths.

04:19 But yeah, keep those limitations in mind, basically.

04:23 And that’s all about absolute and relative paths with pathlib. In the next lesson, you will learn more about dealing with Path objects and how you can access file path components from those Path objects.

Become a Member to join the conversation.