Reputation: 4251
I want to change dir to the parent dir of my jupyter notebook.
I can not take the notebook path using os.path.basename(os.path.dirname(os.path.realpath(__file__)))
as __file__
is not defined.
How can I get the dir of the ipynb file I am using in order to os.chdir() to it?
Upvotes: 3
Views: 5842
Reputation: 146
You can't
https://github.com/ipython/ipython/issues/10123
The reason is because you're always running in the kernel, and in theory multiple notebooks could connect to that kernel.
However - by default if you're starting a notebook, the current working directory is set to the path of the notebook. So the closest, is to call os.getcwd()
I just created the most boring published notebook example to demonstrate this, you can see that, this notebook reflects it's path, and this one that's in a subdirectory also reflects the proper path
Upvotes: 5