Skip to content

Files

sandbox.files works on the sandbox’s file system through the guest agent. Paths are absolute.

from svm import Sandbox
with Sandbox.create(template="base") as sandbox:
files = sandbox.files
files.make_dir("/tmp/docs")
files.write("/tmp/docs/hello.txt", "hello, sandbox\n")
print(files.read_text("/tmp/docs/hello.txt"))
for entry in files.list("/tmp/docs"):
print(entry.name, entry.type, entry.size)
print(files.exists("/tmp/docs/hello.txt")) # True
info = files.get_info("/tmp/docs/hello.txt")
print(info.path, info.size, info.permissions)
files.rename("/tmp/docs/hello.txt", "/tmp/docs/greeting.txt")
files.remove("/tmp/docs")

write_batch writes several files in one round trip. read returns bytes and accepts an offset and a limit for ranged reads.

For large files, upload_url and download_url give a short-lived signed URL that any HTTP client can use without your API key. Treat the URL as a credential.

from svm import Sandbox
with Sandbox.create(template="base") as sandbox:
sandbox.files.write_batch({
"/tmp/app/main.py": "print('hello')\n",
"/tmp/app/README.md": "# app\n",
})
sandbox.files.write("/tmp/app/blob.bin", bytes(range(256)))
data = sandbox.files.read("/tmp/app/blob.bin") # bytes
print(len(data), data[:4])
# Signed URLs let another machine fetch or send a file without the API key.
download = sandbox.files.download_url("/tmp/app/main.py", expires_in_seconds=600)
upload = sandbox.files.upload_url("/tmp/app/upload.txt")
print("download:", download.url, "until", download.expires_at)
print("upload:", upload.url)

watch_dir streams create, write, rename and remove events for a directory.

from svm import Sandbox
with Sandbox.create(template="base") as sandbox:
sandbox.files.make_dir("/tmp/inbox")
# Another process writes into the directory while we watch it.
sandbox.commands.run(
"sleep 1; for i in 1 2 3; do echo $i > /tmp/inbox/file$i.txt; sleep 0.5; done",
background=True,
)
for event in sandbox.files.watch_dir("/tmp/inbox"):
print(event.type, event.name)
if event.name == "file3.txt":
break

Reading a path that does not exist raises the SDK’s file-not-found error:

SDK Error
Python SandboxFileNotFoundError
TypeScript FileNotFoundError
Go ErrFileNotFound
Ruby FileNotFoundError
Java ProcessException.FileNotFound

A full disk raises NotEnoughSpaceError. See Errors and retries.