Sub topic 8
Copying and listing files from a notebook
This chapter assumes you can run Python and SQL cells in a Databricks notebook. It also assumes you have a volume such as main.default.landing and know the /Volumes/<catalog>/<schema>/<volume>/<path> format. You need WRITE VOLUME to copy files in and READ VOLUME to list them.
Where a notebook file starts
A file you create in a notebook usually begins on the driver node's local disk. Local disk is temporary. It disappears when the cluster stops. A volume is durable and governed by Unity Catalog, so copying the file there makes it available to other users and jobs.
dbutils.fs is the utility that moves files between locations. It tells locations apart by their prefix:
| Prefix | Location |
|---|---|
| `file:/` | Driver local disk |
| `/Volumes/...` | Unity Catalog volume |
| `dbfs:/` | Legacy DBFS |
Copy a file into a volume
Write a small CSV to local disk, then copy it into the volume.
local_path = "/tmp/sales.csv"
with open(local_path, "w") as f:
f.write("order_id,amount\n1,19.99\n2,5.50\n3,42.00\n")
src = "file:/tmp/sales.csv"
dst = "/Volumes/main/default/landing/sales.csv"
dbutils.fs.cp(src, dst)
display(dbutils.fs.ls("/Volumes/main/default/landing/"))Use file:/tmp/sales.csv for the source. Without file:, dbutils.fs treats a bare path as a DBFS path and will not find your file.
dbutils.fs.ls returns a list of FileInfo objects. Each has path, name, size and modificationTime. Directory names end with /.
Other file operations
The same utility handles the rest of the day-to-day work on volume files.
base = "/Volumes/main/default/landing/"
dbutils.fs.mkdirs(base + "archive/")
dbutils.fs.cp(base + "sales.csv", base + "archive/sales_copy.csv")
dbutils.fs.mv(base + "archive/sales_copy.csv", base + "archive/sales_2026.csv")
dbutils.fs.rm(base + "archive/sales_2026.csv")
for item in dbutils.fs.ls(base):
print(item.name, item.size)Pass True as a second argument to dbutils.fs.cp or dbutils.fs.rm to act on a directory recursively. Use rm with care. Deleting from a managed volume is permanent.
Standard Python works too
Volume paths look like ordinary file paths to the driver, so Python's standard libraries also work with them.
import os
import shutil
base = "/Volumes/main/default/landing/"
print(os.listdir(base))
shutil.copy("/tmp/sales.csv", base + "sales_backup.csv")Here the source is a plain /tmp/... path with no file: prefix. That is because shutil and os always work on the local file system. The file: prefix belongs only to dbutils and Spark.
Common errors
PERMISSION_DENIED: you lackREAD VOLUMEorWRITE VOLUMEon the volume. Ask the owner to grant it.java.io.FileNotFoundExceptionon the source: the file is on the driver, but you left offfile:.- Path not found on the volume: check the catalog, schema and volume names. A typo in any one of them fails the whole path.
Exercise
You run dbutils.fs.cp("/tmp/report.csv", "/Volumes/main/default/landing/report.csv") and get a file-not-found error, although /tmp/report.csv exists on the driver. What is the fix?
Show answer
Prefix the source with file:, giving file:/tmp/report.csv. Without a prefix, dbutils.fs looks for the path in DBFS, not on the driver's local disk.