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.

Using Path.home() and Path.cwd()

00:00 Another way to create a Path object is using Path.home() and Path.cwd(). Let’s look at both of them, starting with Path.home().

00:10 This class method points you to the home directory on your computer. Now this can be different things depending on which operating system you’re on. Like I mentioned before, I’m on a macOS system, so for me, if I type pathlib.Path.home(),

00:28 the Path object that I get points to /User/ and then my username, which is martin. If you’re in a different operating system, it will point to a different folder.

00:41 Let’s look at the three different options that you’re likely to encounter. If you’re on a Windows system, then it will point to C:\Users\<username>.

00:53 If you’re on macOS, as you just saw, it points to /Users/<username>. And if you’re on Ubuntu Linux, then it’s going to point to /home/<username>.

01:06 And in all of these cases, you just use Path.home(). So you use the same method in order to get different paths. And which path you get, again, depends on your operating system.

01:17 Now you might already think about that this is a useful way of handling programs that should run cross-platform. If you do rely on, for example, creating a folder in the user’s home directory, then you can use Path.home() instead of trying to hard-code any sort of value, which wouldn’t work in the first place because people have different usernames but also wouldn’t work because of the different paths depending on the different operating systems. However, if you use Path.home(), then this will automatically use the correct directory for the specific user on their operating system.

01:55 Path.cwd(), which stands for current working directory, also creates a Path object in a way dynamically. And in this case it’s a little bit different, though, because Path.cwd() is a dynamic reference to a directory where currently a process is running.

02:14 And this can change over the lifetime of a program, and it can also be different depending on where you’re currently running Python. Let’s take a look over in IDLE.

02:25 If I run pathlib.Path.cwd()

02:29 in my IDLE REPL, then I get back /Users/<username>/Documents/. This is the default value that IDLE sets for the current working directory when you start it from your operating system’s graphical user interface. However, that’s not consistent across other runs of programs.

02:54 As you just saw, when you’re running IDLE, then usually this dynamic reference points to your Documents/ folder. When you’re running a script, then it usually points to the folder that contains your script.

03:05 But keep in mind that this is a dynamic reference, so it might be a little hard to reason where it will point to during the run of your program, and because you can’t control exactly how your users are going to run the program, this might also vary between them, so it’s a little tricky, and there’s a third point to it, which is that this value can also change over the lifetime of your program.

03:29 If you do some sort of manipulations in your script, then the current working directory could be something else at the end of the script than it is at the beginning. So it is a bit tricky.

03:38 Now you know how it works, and use it with caution.

Become a Member to join the conversation.