1
Curious builder0 XP earned · 300 to level 2
0 daysFinish a lesson to begin
Badge collection0 of 6 unlocked
10 small wins to finish your pathNext lesson →
Files and shell commands
sandbox.files uploads and downloads files, and sandbox.commands runs shell commands, so an agent can work with a customer's CSV or install a package.
with open("orders.csv", "rb") as f:
sandbox.files.write("/home/user/orders.csv", f)
execution = sandbox.run_code("import pandas as pd\npd.read_csv('/home/user/orders.csv').total.sum()")
report = sandbox.files.read("/home/user/report.md")import inspect
from e2b.sandbox_sync.commands.command import Commands
from e2b.sandbox_sync.filesystem.filesystem import Filesystem
for method in [Filesystem.write, Filesystem.read, Commands.run]:
parameters = inspect.signature(method).parameters
print(method.__qualname__, [name for name in parameters if name != "self"])
print("Commands.run timeout default:", inspect.signature(Commands.run).parameters["timeout"].default)From the installed SDK: files.write takes a path and data as text, bytes or a file object; files.read returns text unless format is bytes or stream. commands.run takes a shell command, optional environment variables, a working directory and a user, and times out after 60 seconds by default, unlike run_code's 300.
result = sandbox.commands.run("pip install openpyxl", timeout=120)
print(result.exit_code, result.stdout[-200:])Paths are in the sandbox
/home/user is the sandbox user's home. Nothing written there reaches your server unless you read it back, and it disappears when the sandbox is killed.Try it yourself
- Print the parameters of
Filesystem.list. - Find what
Commands.runreturns in its signature's return annotation. - Print the signature of
Filesystem.write_files.
Little by little, you're building something great.