Read and Write Files
Read file contents from and write files to the sandbox filesystem using the Python SDK.
The Agent Sandbox Python SDK (k8s-agent-sandbox) provides a files API on every sandbox instance for interacting with the sandbox filesystem. You can read and write files, list directories, check if paths exist, and upload or download data — all through the SDK without needing kubectl exec.
All file operations are also available as async methods via AsyncSandboxClient.
pip install k8s-agent-sandbox.SandboxTemplate named python-sandbox-template applied to your cluster. A SandboxTemplate defines the pod spec (image, resources, probes, optional runtimeClassName for gVisor/Kata) used when a sandbox is created. It must exist in the target namespace before create_sandbox(template=...) will succeed — otherwise the call returns a NotFound error.Apply this minimal template once per namespace:
kubectl apply -n default -f - <<'EOF'
apiVersion: extensions.agents.x-k8s.io/v1alpha1
kind: SandboxTemplate
metadata:
name: python-sandbox-template
spec:
podTemplate:
spec:
containers:
- name: python-runtime
image: us-central1-docker.pkg.dev/k8s-staging-images/agent-sandbox/python-runtime-sandbox:latest-main
ports:
- containerPort: 8888
readinessProbe:
httpGet: { path: "/", port: 8888 }
periodSeconds: 1
restartPolicy: OnFailure
EOF
The full template (with isolation runtime options) lives at clients/python/agentic-sandbox-client/python-sandbox-template.yaml in the repository.
from k8s_agent_sandbox import SandboxClient
client = SandboxClient()
sandbox = client.create_sandbox(template="python-sandbox-template", namespace="default")
sandbox.files.write("/home/user/hello.txt", "Hello from the SDK!")
content = sandbox.files.read("/home/user/hello.txt")
print(content.decode()) # 'Hello from the SDK!'
entries = sandbox.files.list("/home/user")
for entry in entries:
print(f"{entry.name} ({entry.type}, {entry.size} bytes)")
if sandbox.files.exists("/home/user/hello.txt"):
print("File exists!")
sandbox.terminate()