The capsule.files interface gives you direct access to the capsule’s filesystem. You can write source files before running them, read output files after a command finishes, inspect directory trees, and stream large files in and out without buffering them in memory.
Writing files
write() accepts either a string or bytes. When you pass a string, it is UTF-8 encoded automatically. Parent directories are created if they do not exist.
Reading files
read() returns the file as a UTF-8 string. read_bytes() returns the raw bytes.
Checking existence
Listing directory contents
list() returns a list of FileEntry objects representing the immediate children of a directory. Pass depth to recurse further.
Each FileEntry has:
Creating directories
make_dir() creates a directory and all required parent directories. It is idempotent — calling it on an existing path does not raise an error.
Removing files and directories
remove() deletes a file or directory recursively.
remove() is recursive. Passing a directory path deletes the directory and everything inside it.
Streaming large files
For files larger than a few megabytes, use the streaming methods to avoid loading the entire file into memory.
Streaming upload
upload_stream() accepts any iterator that yields bytes chunks:
Streaming download
download_stream() yields successive byte chunks:
Use upload_stream and download_stream for any file over a few MB. Buffering large files in memory can exhaust the host process’s heap and cause requests to time out.
Complete example