What You’ll Learn
- Creating directories with
sbx.commands.run("mkdir -p ...")
- Writing and executing a file-generating script
- Listing output files with
sbx.files.list()
- Reading multiple files with
sbx.files.read() in a loop
- Iterating over
EntryInfo objects (name, type, size, path)
Prerequisites
This example is available in Python. TypeScript version coming soon.
Code Walkthrough
Create an output directory inside the sandbox:
from declaw import Sandbox
sbx = Sandbox.create(template="python", timeout=300)
try:
sbx.commands.run("mkdir -p /tmp/output")
print("Created /tmp/output")
Write and run a generator script that produces a JSON report and a CSV:
generator_script = '''
import json
import datetime
report = {
"generated_at": datetime.datetime.now().isoformat(),
"status": "complete",
"metrics": {
"accuracy": 0.95,
"precision": 0.93,
"recall": 0.97,
"f1_score": 0.95
},
"summary": "Model evaluation complete. All metrics above threshold."
}
with open("/tmp/output/report.json", "w") as f:
json.dump(report, f, indent=2)
with open("/tmp/output/predictions.csv", "w") as f:
f.write("id,predicted,actual,correct\\n")
for i in range(10):
pred = i % 3
actual = i % 3 if i != 7 else (i % 3 + 1) % 3
f.write(f"{i},{pred},{actual},{pred == actual}\\n")
print("Files generated successfully!")
'''
sbx.files.write("/tmp/generate.py", generator_script)
result = sbx.commands.run("python3 /tmp/generate.py")
print(f"stdout: {result.stdout}")
List the output directory — each entry has name, type, size, and path:
entries = sbx.files.list("/tmp/output")
for entry in entries:
print(f" {entry.name} ({entry.type}, {entry.size} bytes)")
Read each file back using the path attribute from the directory listing:
for entry in entries:
print(f"\n--- Reading {entry.name} ---")
content = sbx.files.read(entry.path)
print(content)
finally:
sbx.kill()
Expected Output
==================================================
Declaw Download Results Example
==================================================
--- Creating Sandbox ---
Sandbox created: sbx_abc123
--- Creating Output Directory ---
Created /tmp/output
--- Writing Generator Script ---
Wrote /tmp/generate.py
--- Running Generator Script ---
stdout: Files generated successfully!
--- Listing Output Files ---
report.json (file, 245 bytes)
predictions.csv (file, 178 bytes)
--- Reading report.json ---
{
"generated_at": "2026-04-02T12:00:00.000000",
"status": "complete",
"metrics": {
"accuracy": 0.95,
"precision": 0.93,
"recall": 0.97,
"f1_score": 0.95
},
"summary": "Model evaluation complete. All metrics above threshold."
}
--- Reading predictions.csv ---
id,predicted,actual,correct
0,0,0,True
1,1,1,True
...
--- Cleaning Up ---
Sandbox killed.
==================================================
Done!
==================================================