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

# File Operations

> Write, read, check, list, rename, batch-write, and remove files inside a Declaw sandbox.

## What You'll Learn

* Writing a file with `sbx.files.write()`
* Reading a file with `sbx.files.read()`
* Checking file existence with `sbx.files.exists()`
* Getting file metadata with `sbx.files.get_info()` / `sbx.files.getInfo()`
* Listing directory contents with `sbx.files.list()`
* Creating directories with `sbx.files.make_dir()` / `sbx.files.makeDir()`
* Renaming files with `sbx.files.rename()`
* Batch writing with `sbx.files.write_files()` / `sbx.files.writeFiles()`
* Removing files with `sbx.files.remove()`

## Prerequisites

<Snippet file="snippets/env-setup.mdx" />

## Code Walkthrough

<Tabs>
  <Tab title="Python">
    Import `WriteEntry` for batch operations:

    ```python theme={null}
    from declaw import Sandbox, WriteEntry
    ```

    **Write and read a file:**

    ```python theme={null}
    sbx.files.write("/tmp/hello.txt", "Hello, Declaw!")
    content = sbx.files.read("/tmp/hello.txt")
    print(f"Content: {content}")  # Hello, Declaw!
    ```

    **Check existence and get metadata:**

    ```python theme={null}
    exists = sbx.files.exists("/tmp/hello.txt")
    print(f"Exists: {exists}")  # True

    info = sbx.files.get_info("/tmp/hello.txt")
    print(f"Name: {info.name}")   # hello.txt
    print(f"Type: {info.type}")   # FileType.FILE
    print(f"Size: {info.size}")   # 14
    ```

    **List directory:**

    ```python theme={null}
    entries = sbx.files.list("/tmp")
    for entry in entries:
        print(f"  {entry.name} ({entry.type})")
    ```

    **Make a directory, rename a file:**

    ```python theme={null}
    sbx.files.make_dir("/tmp/mydir")
    sbx.files.rename("/tmp/hello.txt", "/tmp/renamed.txt")
    ```

    **Batch write multiple files at once:**

    ```python theme={null}
    sbx.files.write_files([
        WriteEntry(path="/tmp/a.txt", data="aaa"),
        WriteEntry(path="/tmp/b.txt", data="bbb"),
    ])
    ```

    **Remove a file and verify:**

    ```python theme={null}
    sbx.files.remove("/tmp/renamed.txt")
    exists = sbx.files.exists("/tmp/renamed.txt")
    print(f"Exists: {exists}")  # False
    ```
  </Tab>

  <Tab title="TypeScript">
    Import `WriteEntry` type:

    ```typescript theme={null}
    import { Sandbox } from "@declaw/sdk";
    import type { WriteEntry } from "@declaw/sdk";
    ```

    **Write and read a file:**

    ```typescript theme={null}
    await sbx.files.write("/tmp/hello.txt", "Hello, Declaw!");
    const content = await sbx.files.read("/tmp/hello.txt");
    console.log(`Content: ${content}`);  // Hello, Declaw!
    ```

    **Check existence and get metadata:**

    ```typescript theme={null}
    const exists = await sbx.files.exists("/tmp/hello.txt");
    console.log(`Exists: ${exists}`);  // true

    const info = await sbx.files.getInfo("/tmp/hello.txt");
    console.log(`Name: ${info.name}`);   // hello.txt
    console.log(`Type: ${info.type}`);   // file
    console.log(`Size: ${info.size}`);   // 14
    ```

    **List directory:**

    ```typescript theme={null}
    const entries = await sbx.files.list("/tmp");
    for (const entry of entries) {
      console.log(`  ${entry.name} (${entry.type})`);
    }
    ```

    **Make a directory, rename a file:**

    ```typescript theme={null}
    await sbx.files.makeDir("/tmp/mydir");
    await sbx.files.rename("/tmp/hello.txt", "/tmp/renamed.txt");
    ```

    **Batch write multiple files:**

    ```typescript theme={null}
    const files: WriteEntry[] = [
      { path: "/tmp/a.txt", data: "aaa" },
      { path: "/tmp/b.txt", data: "bbb" },
    ];
    await sbx.files.writeFiles(files);
    ```

    **Remove and verify:**

    ```typescript theme={null}
    await sbx.files.remove("/tmp/renamed.txt");
    const stillExists = await sbx.files.exists("/tmp/renamed.txt");
    console.log(`Exists: ${stillExists}`);  // false
    ```
  </Tab>
</Tabs>

## Expected Output

```
==================================================
Declaw File Operations Example
==================================================

--- Creating Sandbox ---
Sandbox created: sbx_abc123

--- Section 1: Write File ---
Wrote /tmp/hello.txt

--- Section 2: Read File ---
Content: Hello, Declaw!

--- Section 3: Check Exists ---
Exists: True

--- Section 4: Get Info ---
Name: hello.txt
Type: FileType.FILE
Size: 14

--- Section 5: List Directory ---
  hello.txt (file)
  ...

--- Section 6: Make Directory ---
Created /tmp/mydir

--- Section 7: Rename File ---
Renamed /tmp/hello.txt -> /tmp/renamed.txt

--- Section 8: Batch Write ---
Batch wrote /tmp/a.txt and /tmp/b.txt

--- Section 9: Remove File ---
Removed /tmp/renamed.txt

--- Section 10: Verify Removed ---
Exists: False

--- Cleaning Up ---
Sandbox killed.

==================================================
Done!
==================================================
```
