cancel
Showing results for 
Search instead for 
Did you mean: 
Data Engineering
Join discussions on data engineering best practices, architectures, and optimization strategies within the Databricks Community. Exchange insights and solutions with fellow data engineers.
cancel
Showing results for 
Search instead for 
Did you mean: 

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.

2 REPLIES 2

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.

Connect with Databricks Users in Your Area

Join a Regional User Group to connect with local Databricks users. Events will be happening in your city, and you won’t want to miss the chance to attend and share knowledge.

If there isn’t a group near you, start one and help create a community that brings people together.

Request a New Group