Overview
File reading is essential for:- Accessing generated outputs from code execution
- Reading configuration files
- Processing data files
- Retrieving logs and results
Files can be read from allowed paths like
/workspace and /tmp. Reading from system directories may be restricted for security.Reading Text Files
Read text files as strings:- Python
- JavaScript
Reading Binary Files
Read binary files (images, PDFs, etc.) as bytes:- Python
- JavaScript
Reading Code Files
Read and execute code files:- Python
- JavaScript
Reading Configuration Files
Read JSON, YAML, or other configuration files:- Python
- JavaScript
Reading Large Files
For large files, consider using timeouts:- Python
- JavaScript
Error Handling
Handle file reading errors:- Python
- JavaScript
Reading Multiple Files
Read multiple files in a loop:- Python
- JavaScript
Complete Example
Here’s a complete example showing file reading workflow:- Python
- JavaScript
Best Practices
1
1. Check File Existence
Use
files.exists() before reading to avoid errors, or handle FileNotFoundError appropriately.2
2. Use Appropriate Method
Use
read() for text files and read_bytes() for binary files (images, PDFs, etc.).3
3. Set Timeouts for Large Files
For large files, set appropriate timeouts to avoid connection timeouts.
4
4. Handle Encoding
Text files are automatically decoded. Binary files return raw bytes.
5
5. Read from Allowed Paths
Only read from allowed paths like
/workspace and /tmp. System directories may be restricted.Related
- Writing Files - Write files to sandboxes
- Listing Files - List directory contents
- SDK: sandbox.files.read() - Python SDK method
- API: GET /files/read - VM Agent API endpoint
Next Steps
- Learn about Writing Files to create files
- Explore Listing Files to browse directories
- Review Downloading Files to save files locally

