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.

tonypy on Feb. 20, 2023

Path.home() in Windows (11) results in WindowsPath(‘C:/Users/<username>’, not WindowsPath(‘C:\Users\<username>’) as written in this slide.

Martin Breuss RP Team on Feb. 21, 2023

@tonypy that’s how Python represents the WindowsPath object. The slide shows what the actual path looks like on the different operating systems. Python internally represents all Path objects with forward slashes (/).

However, if you print out the WindowsPath object, or convert it via str(your_windows_path), then you should see the output containing the backslash character (\) instead.

tonypy on Feb. 21, 2023

Thanks for the feedback. Yes, I agree, it was just an observation that there was a difference between what slide shows (WindowsPath(‘C:\Users\<username>’)) and what actually happens when entering Path.home() in Windows (WindowsPath(‘C:/Users/<username>’). Howeve, as you say, when I go into File Explorer and copy the address I get C:\Users\<username>. It may be worth expanding on that on the slide just to clarify for those using Windows. One other suggestion. Where students don’t use a directory of C:\Users\<username> for following along with the lessons on their PCs, their path may well use names that employ spaces. So the path name needs to be enclosed in “” e.g. “D:\Python\Real Python\Python Basics\File System Operations”. It may be worth pointing that out as well.

Become a Member to join the conversation.