Invalid Path when getting Notebook Path

Steve_Harrison
New Contributor III

The undocumented feature to get a notebook path as is great but it does not actually return a valid path that can be used in python, e.g.:

from pathlib import Path

print(Path(dbutils.notebook.entry_point.getDbutils().notebook().getContext().notebookPath().get()).exists())

Always returns False. Is there a method of getting the actual path of a notebook?

The above suffers from two problems:

  1. It appears to strip off the /Workspace from the start of the path
  2. It removes the suffix from the path

2 is not so much of an issue but the first one is a royal pain.

Walter_C
Databricks Employee
Databricks Employee

The issue you're encountering with dbutils.notebook.entry_point.getDbutils().notebook().getContext().notebookPath().get() not returning a valid path that can be used in Python is known. This method indeed strips off the /Workspace from the start of the path and removes the suffix, which causes the Path.exists() check to fail.

To get the actual path of a notebook, you can prepend /Workspace to the path returned by getDbutils().notebook().getContext().notebookPath().get(). Here is an example of how you can modify your code to achieve this:

from pathlib import Path
import os

# Get the notebook path
notebook_path = dbutils.notebook.entry_point.getDbutils().notebook().getContext().notebookPath().get()

# Prepend '/Workspace' to the path
full_notebook_path = '/Workspace' + notebook_path

# Check if the path exists
print(Path(full_notebook_path).exists())

 

Steve_Harrison
New Contributor III

I actually think the major issue is that the above is undocumented and not supported. A supported and documented way of doing this would be much appreciated.