Skip to main content
Read files from your sandbox filesystem. HopX supports both text and binary file reading with automatic encoding handling.

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:
Expected Output:

Reading Binary Files

Read binary files (images, PDFs, etc.) as bytes:
Expected Output:

Reading Code Files

Read and execute code files:
Expected Output:

Reading Configuration Files

Read JSON, YAML, or other configuration files:
Expected Output:

Reading Large Files

For large files, consider using timeouts:
Expected Output:

Error Handling

Handle file reading errors:
Expected Output:

Reading Multiple Files

Read multiple files in a loop:
Expected Output:

Complete Example

Here’s a complete example showing file reading workflow:

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.

Next Steps