Skip to main content
The Filesystem module provides full file I/O within a sandbox. Access it via sandbox.files.

Reading Files

await sandbox.files.read(path)

Reads a file as a UTF-8 string.
content = await sandbox.files.read('/app/config.json')
config = json.loads(content)
path
str
required
Absolute path to the file.
Returns: str

await sandbox.files.read_bytes(path)

Reads a file as raw bytes.
data = await sandbox.files.read_bytes('/app/image.png')
print(f"File size: {len(data)} bytes")
path
str
required
Absolute path to the file.
Returns: bytes

Writing Files

await sandbox.files.write(path, content)

Writes a string or bytes to a file. Creates parent directories if needed.
# Write text
await sandbox.files.write('/app/hello.txt', 'Hello, World!')

# Write binary
await sandbox.files.write('/app/data.bin', b'\x00\x01\x02')
path
str
required
Absolute path to the file.
content
str | bytes
required
File content to write.
Returns: None

Listing Directories

await sandbox.files.list(path)

Lists the contents of a directory.
entries = await sandbox.files.list('/app')

for entry in entries:
    kind = 'd' if entry.is_dir else 'f'
    print(f"{kind} {entry.name} ({entry.size} bytes)")
path
str
default:"/"
Directory path to list.
Returns: list[EntryInfo]

EntryInfo

EntryInfo is a dataclass:
FieldTypeDescription
namestrFile or directory name
is_dirboolTrue if this entry is a directory
pathstrFull path to the entry
sizeintSize in bytes (0 for directories)

Managing Files and Directories

await sandbox.files.make_dir(path)

Creates a directory and any necessary parent directories.
await sandbox.files.make_dir('/app/src/components')
path
str
required
Directory path to create.
Returns: None

await sandbox.files.remove(path)

Deletes a file or directory.
await sandbox.files.remove('/app/temp.txt')
path
str
required
Path to remove.
Returns: None

await sandbox.files.exists(path)

Checks whether a file or directory exists.
if await sandbox.files.exists('/app/requirements.txt'):
    print('Python project detected')
path
str
required
Path to check.
Returns: bool

Examples

Upload and Run a Script

await sandbox.files.write('/tmp/setup.sh', """#!/bin/bash
echo "Running setup..."
apt-get update -qq
echo "Done!"
""")

result = await sandbox.commands.run('bash /tmp/setup.sh')
print(result.stdout)

Copy Files Between Paths

content = await sandbox.files.read_bytes('/app/original.dat')
await sandbox.files.write('/backup/original.dat', content)