The examples on this page omit
auth. To authenticate with your W&B API key, install cwsandbox[wandb] and pass auth=AuthStrategy.WANDB to Sandbox.run(), Sandbox(), or Sandbox.session(). See Get started.Basic operations
The examples in this section cover the most common file operations: writing data to a path in the sandbox and reading data back. File operations returnOperationRef objects. Use .result() to block for completion.
Write files
Read files
Parallel operations
File operations return immediately, enabling natural parallelism. Running uploads or downloads in parallel can reduce total transfer time when you have multiple independent files.Parallel uploads
Parallel downloads
Upload-process-download pattern
A common workflow: upload input files, run processing, download results.Error handling
File operations can fail if a path doesn’t exist or isn’t writable. CatchSandboxFileError to handle these cases and inspect the affected path.
File not found
Write errors
Binary files
File operations work with any binary content, including images and serialized Python objects:Text encoding
Files are transferred as bytes, so you must encode and decode text explicitly to avoid corruption with non-ASCII characters.Large file considerations
Theread_file and write_file APIs are best suited for small to moderately sized files. For large files, consider the following:
- Account for bandwidth, because files are transferred through the API.
- Use streaming for large datasets when possible.
- Access object storage directly instead of transferring the data through the API.