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

# Write to Files Inside Your Sandbox Instance

> Write text or binary files to the sandbox filesystem with automatic directory creation using the Python SDK.

The `write_file()` method allows you to create or overwrite files inside your sandbox's isolated filesystem.\
It supports both **text** and **binary** content, making it flexible for handling scripts, configuration files, and assets such as images, JSON files, or compiled binaries.

All file paths must begin with the absolute sandbox path `/home/damner`.

***

### `async write_file(path: str, content: Union[str, bytes]) -> None`

**Description:**\
Writes a file to the sandbox filesystem. Supports both text (`str`) and binary (`bytes`) content.\
Automatically creates directories if they don't exist.

***

**Parameters:**

| Parameter | Type             | Required | Description                                |
| --------- | ---------------- | -------- | ------------------------------------------ |
| `path`    | `str`            | Yes      | File path (must start with `/home/damner`) |
| `content` | `str` or `bytes` | Yes      | File content (text string or binary data)  |

***

**Returns:**\
`None` : Resolves when the file write operation is complete.

***

**Examples**

```python theme={null}
import asyncio
import json
from fermion_sandbox import Sandbox

async def main():
    sandbox = Sandbox(api_key="your-api-key")
    await sandbox.create(should_backup_filesystem=False)

    # Write a simple Python file
    await sandbox.write_file(
        path="/home/damner/script.py",
        content='print("Hello World")'
    )

    # Multi line content
    config = {
        "name": "my-app",
        "version": "1.0.0"
    }

    await sandbox.write_file(
        path="/home/damner/config.json",
        content=json.dumps(config, indent=2)
    )

asyncio.run(main())
```

**Path Handling**

```python theme={null}
# Valid paths (must start with /home/damner)

await sandbox.write_file(path="/home/damner/file.txt", content="data")
await sandbox.write_file(path="/home/damner/code/file.txt", content="data")
await sandbox.write_file(path="/home/damner/nested/dir/file.txt", content="data")

# Invalid paths
await sandbox.write_file(path="/tmp/file.txt", content="data")  # Error
await sandbox.write_file(path="relative/file.txt", content="data")  # Error
await sandbox.write_file(path="./file.txt", content="data")  # Error
```
