Knowing where you are is important, whether you are in the outdoors, stumbling to the toilet in the middle of the night, or simply programming. Dealing with your midnight confusing or outdoors route-finding skills are a little more complicated, but fortunately, it is pretty easy to figure out your current path in Python. 🐍
Method 1: pathlib.cwd()
To get your current path in Python, use the
pathlib module in the python standard library and call
cwd() that’s an abbreviation for “current working directory”. If you need your path and the file from which you are calling use
from pathlib import Path print(Path.cwd()) # Out: C:\Users\esimm\PythonDev\notebooks
This code imports the
Path class, and prints the working directory. The
Path class is an object-oriented approach to working with your path. Getting your current working directory is pretty easy, and there are a few other ways to accomplish the same thing.
Method 2: os.getcwd()
One older way to do the same thing in Python is to use
os.getcwd(), which is useful if you want to interact with your path as a string. Alternatively, you could query the global variables of your Python environment.
import os cwd = os.getcwd() print(cwd) # Out: c:\Users\esimm\PythonDev\notebooks
Now, this code tells you what you’re working directory is. Both of these examples are very similar to opening a shell prompt and calling the
cwd command. However what if you want to know what room of the house you are in, and the thing you are working on?
from pathlib import Path path = Path(__file__) print(path)
This prints the full path to the file you are working on. Now before you leave this page there are a couple of caveats, I want to tell you about the variable
__file__ which is set by the Python interpreter
__file__is callable while working in a file. Therefore, it will not work if you try to call it from the shell interpreter.
__file__does not work in the context of a Jupyter notebook, this led me down a rabbit hole which I will revisit soon.
The important thing to know about
__file__ is that it is set by the interpreter at run time, so that python knows what file it is working with when the script is working with multiple modules. The upside is that calling
Path(__file__) gives you a string of the current file you are working on and the path. If you desire you can play with some other methods, such as
About Jupyter Notebook Error
I don’t know about you but I got very interested when calling
__file__ threw an error in my Jupyter notebook. I was so interested; I went on a rabbit trail of trying to figure out why. After all, it is sometimes nice to have a vague idea of how your IDE works. To be clear I am using the VS Code Jupyter extension. This might be similar or different from the IDE that you are using.
I started trying to figure out why
__file__ wasn’t working by experimenting with the following code:
import os cur = globals()['_dh'] name = 'pathBlog.ipynb' jupPath = os.path.join(cur , name) print(cur) print(jupPath)
This code looks up the dictionary of global variables in the current environment and then concatenates the file name of the Jupyter notebook file I am using. Interestingly I almost found another way of printing the current path and file I am working on.
However, the file path is completely wrong if I am using Vs Code and my Jupyter extension. I do all of my work under
<users\esimm\PythonDev\>. This code returned that it was working out of
<Root:\Users\esimm\AppData\Local\Microsoft VS Code>. What is funnier is that the directory listed does not have a copy of my Jupyter file. It must be there temporarily or something.
This means that the extension I am using does not run Jupyter code in the place I have saved it but in this other directory.
The next step was to take a look at the Jupyter docs to see how notebooks are architected (https://jupyter.readthedocs.io/en/latest/projects/architecture/content-architecture.html).
This does not explain the exact details of how my VS Code extension works, but it does give me an idea of how Jupyter notebooks runs code. The layman’s takeaway is that a jupyter notebook is not a source code file, but a JSON file, which is interpreted into code and metadata by the Jupiter server. The server executes the code in the location of the jupyter server and then returns the code and outputs (textual or visual) to the location of the jupyter client.
This concludes the tutorial for today. The moral of the story is that it is important to know what directory you are working in and a common task, thankfully Python makes that pretty easy. There are multiple ways to find your working directory, and the name of the file you are working on. The method used here of using
pathlib is very easy, if you so desire you can find another more complicated way to accomplish this task. Of course, your OS and development environment may make this more difficult especially if you are not aware of how your IDE works or is set up.
I am a Python freelancer and a Methodist Pastor in Seattle Washington. I do programming and ministering to the homeless downtown. I also love to spend time with my family and embark on ambitious outdoor adventures.