Skip to main content
Read, write, and manage files inside a running sandbox. Operations go through the same host-guest channel as command execution: no SSH, no network. See Filesystem for usage examples. For bulk file movement, prefer a volume.

Typical flow

The handle is reached through the sb.fs property on a running Sandbox. All methods are coroutines, so await each call.

Methods

fs.read()

Read the entire contents of a file as raw bytes.

Parameters

pathstr
Absolute path inside the guest, e.g. “/app/config.json”.

Returns

bytes
File contents as raw bytes.

fs.read_text()

Read the entire contents of a file and decode it as UTF-8.

Parameters

pathstr
Absolute path inside the guest.

Returns

str
File contents decoded as UTF-8.

fs.read_stream()

Open a streaming reader for a file. Use this for files too large to hold in memory. The returned FsReadStream is an async iterator that yields chunks of bytes, or call its collect() to gather everything into one bytes.

Parameters

pathstr
Absolute path inside the guest.

Returns

Async iterator yielding chunks of file data.

fs.write()

Write content to a file, creating it if it doesn’t exist and overwriting it if it does.

Parameters

pathstr
Absolute path inside the guest.
databytes
File content.

fs.write_stream()

Open a streaming writer for a file. Use this for files too large to hold in memory. The returned FsWriteSink supports the async context manager protocol, so async with closes and finalizes the file automatically.

Parameters

pathstr
Absolute path inside the guest.

Returns

Async writer that accepts chunks of bytes.

fs.list()

List the entries in a directory.

Parameters

pathstr
Absolute directory path inside the guest.

Returns

Directory entries.

fs.mkdir()

Create a directory, including any missing parent directories.

Parameters

pathstr
Absolute directory path inside the guest.

fs.stat()

Get detailed metadata for a file or directory.

Parameters

pathstr
Absolute path inside the guest.

Returns

File metadata.

fs.exists()

Check whether a path exists inside the sandbox.

Parameters

pathstr
Absolute path inside the guest.

Returns

bool
True if the path exists.

fs.remove_dir()

Remove a directory and its contents recursively.

Parameters

pathstr
Absolute directory path inside the guest.

fs.copy()

Copy a file within the sandbox.

Parameters

srcstr
Source path inside the guest.
dststr
Destination path inside the guest.

fs.rename()

Rename or move a file or directory within the sandbox.

Parameters

srcstr
Current path inside the guest.
dststr
New path inside the guest.

fs.remove()

Remove a file. Use remove_dir() for directories.

Parameters

pathstr
Absolute file path inside the guest.

fs.copy_from_host()

Copy a file from the host machine into the sandbox. For transferring many files, consider a bind-mounted volume instead.

Parameters

host_pathstr
Path on the host filesystem.
guest_pathstr
Destination path inside the sandbox.

fs.copy_to_host()

Copy a file from the sandbox out to the host machine.

Parameters

guest_pathstr
Path inside the sandbox.
host_pathstr
Destination path on the host.

Types

FsEntry

Returned by list()

Metadata for a single directory entry, returned by list().

FsEntryKind

Describes FsEntry.kind · FsMetadata.kind

String enum (StrEnum) of filesystem entry types. The kind fields on FsEntry and FsMetadata carry these string values.

FsMetadata

Returned by stat()

Detailed file metadata, returned by stat().

FsReadStream

Returned by read_stream()

Async stream for reading a file in chunks. Obtained via read_stream(). Iterate it with async for chunk in stream:, or call collect() to gather everything at once.

FsWriteSink

Returned by write_stream()

Async writer for streaming data into a file. Obtained via write_stream(). Supports the async context manager protocol, so async with closes the sink on exit.