nayan_wylde
Esteemed Contributor II

Apps don’t mount /Volumes and don’t ship with dbutils. So os.listdir('/Volumes/...') or dbutils.fs.ls(...) won’t work inside an App. Use the Files API or Databricks SDK instead to read/write UC Volume files, then work on a local copy.

Code using Python SDK to read and download file.

# requirements.txt
# databricks-sdk>=0.50  (or your pinned version)

import os, tempfile, shutil
from databricks.sdk import WorkspaceClient

# If you used App resources + valueFrom, fetch from env; otherwise hardcode the 3-level name.
CATALOG  = os.environ.get("ASSETS_CATALOG",  "main")
SCHEMA   = os.environ.get("ASSETS_SCHEMA",   "default")
VOLUME   = os.environ.get("ASSETS_VOLUME",   "assets")

# The absolute UC Volumes path to your asset:
volume_path = f"/Volumes/{CATALOG}/{SCHEMA}/{VOLUME}/models/weights.bin"

w = WorkspaceClient()  # in Apps, credentials are auto-injected

# Stream the file down to local disk to avoid loading hundreds of MB into RAM
resp = w.files.download(volume_path)   # Files API via SDK
local_tmp = os.path.join(tempfile.gettempdir(), os.path.basename(volume_path))

with open(local_tmp, "wb") as out:
    # resp.contents is a file-like object; copy in 1MiB chunks
    shutil.copyfileobj(resp.contents, out, length=1024*1024)

# Now use your local file with standard libs (torch, PIL, etc.)

----------------------------------------------------------------------

code to list files in volume

import json
from databricks.sdk import WorkspaceClient

w = WorkspaceClient()

dir_path = f"/Volumes/{CATALOG}/{SCHEMA}/{VOLUME}/models/"
# Call Files API: GET /api/2.0/fs/directories{directory_path}
r = w.api_client.do(
    "GET",
    f"/api/2.0/fs/directories{dir_path}",
    # you can pass query params like {"page_size": 1000, "page_token": "..."} if needed
)

entries = r.get("contents", [])
for e in entries:
    print(e["name"], "(dir)" if e.get("is_directory") else f"({e.get('file_size', 0)} bytes)")

--------------------------------------------------

Code to upload file in volume.

from databricks.sdk import WorkspaceClient
import io

w = WorkspaceClient()
target = f"/Volumes/{CATALOG}/{SCHEMA}/{VOLUME}/incoming/config.json"

data = io.BytesIO(b'{"hello":"world"}')
w.files.upload(target, data, overwrite=True)