Skip to main content
Version: 0.3.0
Last Verified: 2025-01-27
Package: hopx-ai on PyPI

Overview

The Files resource provides file operations for sandboxes. Access it via the files property on a Sandbox instance. All file operations are synchronous and work with both text and binary files. The Files resource is lazy-loaded, meaning it’s only initialized when first accessed.

Access

Methods

read

Read text file contents.
Parameters:
  • path (str): File path to read
  • timeout (int, optional): Request timeout in seconds
Returns: str - File contents as string Raises:
  • FileNotFoundError: File does not exist
  • FileOperationError: Read operation failed
Example:
Expected Output:
See Also:

read_bytes

Read binary file contents.
Parameters:
  • path (str): File path to read
  • timeout (int, optional): Request timeout
Returns: bytes - File contents as bytes Example:

write

Write text file contents.
Parameters:
  • path (str): File path to write
  • content (str): Text content to write
  • mode (str, optional): File permissions (default: '0644')
  • timeout (int, optional): Request timeout
Raises:
  • FileOperationError: Write operation failed
Example:
Expected Output:
See Also:

write_bytes

Write binary file contents.
Parameters:
  • path (str): File path to write
  • content (bytes): Binary content to write
  • mode (str, optional): File permissions
  • timeout (int, optional): Request timeout
Example:

list

List directory contents.
Parameters:
  • path (str, optional): Directory path (default: '/workspace')
  • timeout (int, optional): Request timeout
Returns: List[FileInfo] - List of file/directory information objects Example:
Expected Output:
See Also:

exists

Check if file or directory exists.
Parameters:
  • path (str): File or directory path
  • timeout (int, optional): Request timeout
Returns: bool - True if exists, False otherwise Example:
Expected Output:
See Also:

remove

Delete file or directory.
Parameters:
  • path (str): File or directory path to delete
  • timeout (int, optional): Request timeout
Raises:
  • FileNotFoundError: Path does not exist
  • FileOperationError: Delete operation failed
This operation is irreversible. Directories are deleted recursively.
Example:
Expected Output:
See Also:

mkdir

Create directory.
Parameters:
  • path (str): Directory path to create
  • timeout (int, optional): Request timeout
Raises:
  • FileOperationError: Directory creation failed
Example:
Expected Output:

upload

Upload file from local filesystem to sandbox.
Parameters:
  • local_path (str): Local file path
  • remote_path (str): Remote file path in sandbox
  • timeout (int, optional): Request timeout
Example:
Expected Output:

download

Download file from sandbox to local filesystem.
Parameters:
  • remote_path (str): Remote file path in sandbox
  • local_path (str): Local file path
  • timeout (int, optional): Request timeout
Example:
Expected Output:

Examples

Example 1: Basic File Operations

Expected Output:

Example 2: Working with Directories

Expected Output:

Example 3: File Upload/Download

Example 4: Binary File Operations


FileInfo Model

The list() method returns FileInfo objects with the following properties:
  • name (str): File or directory name
  • path (str): Full path
  • size (int): Size in bytes
  • is_directory (bool): Whether it’s a directory
  • permissions (str): File permissions
  • modified_time (datetime): Last modification time
  • is_file (bool): Convenience property (opposite of is_directory)
  • size_kb (float): Size in kilobytes
  • size_mb (float): Size in megabytes

  • Sandbox - Main sandbox class
  • Commands - Command execution resource
  • Models - Data models including FileInfo

See Also

Next Steps