All Products
Search
Document Center

Function Compute:Read and Write Files

Last Updated:Jul 13, 2026

Filesystem is used to manage files in the sandbox. A common workflow is to write input files or code first, run commands, and then read the output files.

Write and read a file

Python example:

import os

from e2b import Sandbox

sandbox = Sandbox.create(
    template="code-interpreter-v1",
    api_key=os.environ["E2B_API_KEY"],
    api_url=os.environ["E2B_API_URL"],
    domain=os.environ["E2B_DOMAIN"],
)

try:
    sandbox.files.write("/tmp/hello.txt", "hello from FC Agent Sandbox\n")
    content = sandbox.files.read("/tmp/hello.txt")
    print(content.strip())
finally:
    sandbox.kill()

TypeScript example:

import { Sandbox } from "e2b";

const sandbox = await Sandbox.create("code-interpreter-v1", {
  apiKey: process.env.E2B_API_KEY,
  apiUrl: process.env.E2B_API_URL,
  domain: process.env.E2B_DOMAIN,
});

try {
  await sandbox.files.write("/tmp/hello.txt", "hello from FC Agent Sandbox\n");
  const content = await sandbox.files.read("/tmp/hello.txt");
  console.log(content.trim());
} finally {
  await sandbox.kill();
}

Write multiple files

The TypeScript SDK can write multiple files in one call. This is useful when an agent needs to place generated code, test files, and configuration files into the sandbox together.

Python example:

sandbox.files.make_dir("/tmp/project")

sandbox.files.write("/tmp/project/main.py", "print('hello')\n")
sandbox.files.write("/tmp/project/README.md", "# Demo\n")
await sandbox.files.makeDir("/tmp/project");

await sandbox.files.write([
  {
    path: "/tmp/project/main.py",
    data: "print('hello')\n",
  },
  {
    path: "/tmp/project/README.md",
    data: "# Demo\n",
  },
]);

write() overwrites an existing file. When you write to a path that does not yet exist, missing parent directories are created automatically. In addition to text, the Python SDK supports bytes and file objects. The TypeScript SDK supports ArrayBuffer, Blob, and ReadableStream. read() returns text by default, and it can also return bytes or streams.

Python example:

sandbox.files.write("/tmp/data.bin", bytes([1, 2, 3]))

data = sandbox.files.read("/tmp/data.bin", format="bytes")
print(len(data))

TypeScript example:

await sandbox.files.write("/tmp/data.bin", new Uint8Array([1, 2, 3]).buffer);

const bytes = await sandbox.files.read("/tmp/data.bin", { format: "bytes" });
console.log(bytes.length);

Supported methods

  • sandbox.files.list(): List directory contents.

  • sandbox.files.exists(): Check whether a file or directory exists.

  • sandbox.files.read(): Read a file.

  • sandbox.files.write(): Write a file.

  • sandbox.files.makeDir(): Create directories.

  • sandbox.files.remove(): Delete a file or directory.

  • sandbox.files.rename(): Move or rename a file or directory.

Recommendations

  • Write temporary inputs, generated code, and intermediate results to /tmp or to an application-defined working directory.

  • For data that must persist across FC Agent Sandbox instances, use external storage or another persistence mechanism instead of relying on the local sandbox filesystem.

  • Before writing user-uploaded files, validate file type, size, and path on the application side.

  • After file operations are complete, still call sandbox.kill() to release the sandbox when the task ends.

Further reading