Getting the Notebook Path in Databricks

Databricks does not support relative paths due to its distributed architecture, which means trying to get the current path will return the Driver path. However, you can use dbutils to achieve similar functionality.

Here’s how you can get the current notebook path using Python:

      %python
      def getCurrentFolderPath():
        import json
        notebookPath = json.loads(dbutils.notebook.entry_point.getDbutils().notebook().getContext().toJson())['extraContext']['notebook_path']
        folderOnly = notebookPath[0:notebookPath.rfind("/")]
        return folderOnly
      print(getCurrentFolderPath())
    

For Scala, you can use:

      %scala
      val notebookPath = dbutils.notebook().getContext().notebookPath.get
      val folderOnly = notebookPath.substring(0, notebookPath.lastIndexOf("/"))
      println(folderOnly)
    

Frequently Asked Questions

Bottom Line: Getting the notebook path in Databricks involves using dbutils due to the lack of support for relative paths. This approach allows you to manage files and paths effectively within your notebooks.


👉 Hop on a short call to discover how Fog Solutions helps navigate your sea of data and lights a clear path to grow your business.