Sub topic 4
Helper utilities with dbutils
This chapter assumes you can run Python cells in a Databricks notebook that is attached to compute. It also assumes you know what a magic command is from the previous chapter.
What dbutils is
dbutils is a built-in object in Databricks notebooks. It groups small helper utilities for tasks that plain Python or SQL does not cover well. These tasks include working with files, reading secrets, passing parameters, and chaining notebooks.
You do not import dbutils in a notebook. It is already defined. You can list its utilities by calling dbutils.help().
dbutils.help()To see the commands in one utility, call help() on that utility, for example dbutils.fs.help().
The utilities you will use most
| Utility | Purpose | Example call |
|---|---|---|
| `dbutils.fs` | List, copy, move, and remove files in storage | `dbutils.fs.ls("/")` |
| `dbutils.widgets` | Create notebook parameters | `dbutils.widgets.get("run_date")` |
| `dbutils.notebook` | Run another notebook and return a value | `dbutils.notebook.exit("ok")` |
| `dbutils.secrets` | Read secrets without showing them in the notebook | `dbutils.secrets.get("scope", "key")` |
Widgets and notebook calls get their own chapters. This chapter focuses on fs and secrets.
Working with files using dbutils.fs
dbutils.fs.ls returns a list of file information objects. Each object has a path, a name, and a size in bytes.
files = dbutils.fs.ls("/databricks-datasets")
for f in files[:5]:
print(f.name, f.size)You can wrap the result in display() to see it as a table.
display(dbutils.fs.ls("/databricks-datasets"))Other common fs commands are mkdirs, cp, mv, head, and rm. The rm command deletes data, and a recursive delete is hard to undo.
dbutils.fs.rm("/tmp/demo_output", True)The second argument True makes the delete recursive. Check the path before you run it.
Reading secrets
A secret is a value such as a password or API token that you store in a secret scope. A secret scope is a named collection of secrets. Use dbutils.secrets so that credentials stay out of notebook code and out of Git.
token = dbutils.secrets.get(scope="my_scope", key="api_token")If you print a secret, Databricks shows [REDACTED] instead of the value. This protects against accidental display, but it is not a security boundary. Anyone who can run code on the compute can find ways to expose the value, so grant access to a scope carefully.
Common errors
NameError: name 'dbutils' is not defined. This happens in plain Python files, such as modules imported from a Git folder.dbutilsexists only in the notebook environment. Pass the values you need into your functions as arguments.- Path confusion.
dbutils.fspaths such as/tmp/datarefer to the Databricks file system. Python'sopen()reads the driver's local disk. A local path fromopen()needs afile:/prefix when used withdbutils.fs. - Missing scope or permission.
secrets.getfails if the scope or key does not exist, or if you lack read access.
Exercise
A function in a Python module stored in your Git folder calls dbutils.secrets.get(...) and fails with a NameError. Why does it fail, and how do you fix it?
Show answer
dbutils is defined only in the notebook environment, not in imported Python modules. Read the secret in the notebook and pass the value to the function as an argument.