Using Path.home() and Path.cwd()
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
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.
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.
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.
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.
Become a Member to join the conversation.