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

# Work with files

> Write inputs, read results, and manage files in a sandbox.

Use `sandbox.files` to work with files inside a sandbox. Paths refer to the
sandbox's filesystem, not your computer.

```python theme={null}
from lithosbox import LithosBox

box = LithosBox()
sandbox = box.sandboxes.create()
sandbox.files.write("/work/message.txt", "Hello from LithosBox\n")
print(sandbox.files.read("/work/message.txt"))
```

`write()` creates missing parent directories. It replaces the entire file if that
path already exists.

## Upload and download

Use `pathlib.Path` to read a local file and send its contents to the sandbox:

```python theme={null}
from pathlib import Path

sandbox.files.write("/work/input.csv", Path("input.csv").read_bytes())
```

After your application creates a result, save it to your computer:

```python theme={null}
Path("result.png").write_bytes(sandbox.files.read_bytes("/work/result.png"))
```

Use `read()` for text and `read_bytes()` for binary files. Uploads support up to
4 MiB per call. Text reads support approximately 16 MiB, and binary reads support
approximately 12 MiB. Use another transfer method for larger files.

## Manage paths

```python theme={null}
sandbox.files.mkdir("/work/results")
sandbox.files.rename("/work/message.txt", "/work/results/message.txt")
print(sandbox.files.list("/work/results"))
print(sandbox.files.exists("/work/results/message.txt"))
```

Remove a file or directory with `remove()`:

```python theme={null}
sandbox.files.remove("/work/results/message.txt")
```

Removing a directory also removes its contents.

## Transfer larger files

For large inputs, download the file from a URL inside the sandbox when possible.
You can also send chunks through a command's standard input. This example uploads
a local file without replacing earlier chunks:

```python theme={null}
source = Path("large-input.bin")
sandbox.run("mkdir -p /work && : > /work/large-input.bin").check()

with source.open("rb") as stream:
    while chunk := stream.read(4 * 1024 * 1024):
        sandbox.run("cat >> /work/large-input.bin", stdin=chunk).check()

size = int(sandbox.run(["wc", "-c", "/work/large-input.bin"]).check().stdout.split()[0])
assert size == source.stat().st_size
```

Use `cat >>` for appending; multiple `files.write()` calls to the same path would
overwrite each other. Verify the size or checksum after a transfer. For large
outputs, upload from the sandbox to storage you control, then download from there.

## Keep or remove files

Files persist between commands and across archive/unarchive. To retain work before
deleting the sandbox, download the files or [save a durable snapshot](/guides/save-restore).

When you are finished:

```python theme={null}
sandbox.delete()
box.close()
```
