How to beginner · 3 min read

E2B filesystem operations

Quick answer
Use the e2b_code_interpreter.Sandbox class to perform filesystem operations securely within a sandbox. You can write files with sandbox.files.write(), run code that accesses these files via sandbox.run_code(), and read or delete files programmatically. Always close the sandbox with sandbox.close() to release resources.

PREREQUISITES

  • Python 3.8+
  • pip install e2b-code-interpreter
  • E2B API key set in environment variable E2B_API_KEY

Setup

Install the e2b-code-interpreter package and set your API key as an environment variable before running filesystem operations.

bash
pip install e2b-code-interpreter
output
Collecting e2b-code-interpreter
  Downloading e2b_code_interpreter-1.0.0-py3-none-any.whl (15 kB)
Installing collected packages: e2b-code-interpreter
Successfully installed e2b-code-interpreter-1.0.0

Step by step

This example demonstrates creating a sandbox, writing a file, running Python code that reads the file, and then closing the sandbox.

python
import os
from e2b_code_interpreter import Sandbox

# Initialize sandbox with API key from environment
sandbox = Sandbox(api_key=os.environ["E2B_API_KEY"] )

# Write a file inside the sandbox filesystem
sandbox.files.write("hello.txt", b"Hello from E2B sandbox!")

# Run Python code that reads the file content
code = '''
with open("hello.txt", "r") as f:
    content = f.read()
print(content)
'''

execution = sandbox.run_code(code)
print("Sandbox output:", execution.text)

# Clean up and close the sandbox
sandbox.close()
output
Sandbox output: Hello from E2B sandbox!

Common variations

  • Use sandbox.run_code() asynchronously with asyncio if needed.
  • Upload multiple files by calling sandbox.files.write() multiple times before running code.
  • Read files back from the sandbox using sandbox.files.read("filename") to retrieve file contents.
  • Run shell commands by passing shell=True to sandbox.run_code() for bash scripts.
python
import os
import asyncio
from e2b_code_interpreter import Sandbox

async def async_example():
    sandbox = Sandbox(api_key=os.environ["E2B_API_KEY"])
    sandbox.files.write("data.csv", b"id,value\n1,100\n2,200")
    code = 'import pandas as pd\ndf = pd.read_csv("data.csv")\nprint(df.sum())'
    execution = await sandbox.run_code(code)
    print("Async sandbox output:", execution.text)
    sandbox.close()

asyncio.run(async_example())
output
Async sandbox output: id       3
value  300
dtype: int64

Troubleshooting

  • If you see API key missing errors, ensure E2B_API_KEY is set in your environment.
  • For FileNotFoundError, verify the file was written before running code.
  • If code execution hangs, check network connectivity and sandbox resource limits.
  • Always call sandbox.close() to avoid resource leaks.

Key Takeaways

  • Use Sandbox from e2b_code_interpreter for secure sandboxed filesystem operations.
  • Write files with sandbox.files.write() and run code accessing them via sandbox.run_code().
  • Always close the sandbox with sandbox.close() to free resources.
  • You can read files back with sandbox.files.read() and run code asynchronously if needed.
  • Set your API key in E2B_API_KEY environment variable before usage.
Verified 2026-04
Verify ↗