> ## Documentation Index
> Fetch the complete documentation index at: /docs/llms.txt
> Use this file to discover all available pages before exploring further.

# Analyse a file an agent was given

> Upload the data, let the agent's code run against it in a sandbox, read the results back.

**You want** an assistant that takes a user's spreadsheet or log file, writes analysis code for it, runs that code, and returns tables or files, without that code ever running on your servers.

**Pieces.** The `python` [template](/docs/concepts/templates.md) has Python 3.12 with numpy, pandas, requests and httpx in `/opt/venv`, which is on the `PATH`. [Files](/docs/guides/files.md) moves data in and out.

**1. Upload, run, read back**

```python analyze_csv.py
from anona_compute import Sandbox

data = "region,sales\nnorth,120\nsouth,80\nnorth,30\n"  # the user's upload

# The code your model wrote for this file.
analysis = """
import pandas as pd
df = pd.read_csv("/home/user/sales.csv")
df.groupby("region")["sales"].sum().to_csv("/home/user/by_region.csv")
"""

with Sandbox.create("python", timeout=300) as sbx:
    sbx.files.write("/home/user/sales.csv", data)
    sbx.files.write("/home/user/analysis.py", analysis.replace("\n    ", "\n"))
    run = sbx.commands.run("python /home/user/analysis.py")
    print(run.exit_code, run.stderr)
    print(sbx.files.read("/home/user/by_region.csv"))
```

If the code fails, `commands.run` raises with the exit code and stderr. Hand that back to the model and let it try again in the same sandbox: the file is still there.

**2. Get files back**

Read text with `files.read`, and binary output such as a chart image with `files.read(path, format="bytes")`. Extra packages install with `pip install` in the sandbox on a plan with public egress, or ship in [your own template](/docs/recipes/custom-environment.md).

**3. Keep state between questions**

For a chat that asks follow-up questions about the same data, keep the sandbox and its loaded files between turns, and [pause it when idle](/docs/recipes/long-running-agent.md). For a stateful Python kernel (variables kept between calls), the `code-interpreter` template runs E2B's code-interpreter server.

**Untrusted output.** The analysis code is your model's, so treat it like [any code an agent wrote](/docs/recipes/run-untrusted-code.md): a short timeout, and egress off unless it needs it.

**Runnable example:** [`examples/python/analyze_csv.py`](https://github.com/anonalabs/anona-compute-sdk/blob/main/examples/python/analyze_csv.py) in the public SDK repository. It prints what happens at each step.


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.