Overview of File Tools
The main file management methods are:- create_file(file_path, content): Create or overwrite a file with the given content.
- read_file(file_path): Read and return the content of a file.
- search_files(query, file_patterns): Search for lines matching a query across files.
Tip: The SDK is designed for text files. For binary files, use encoding/decoding (see below).
Method Reference
1. Creating Files
- file_path (
str
): Path to the file, relative to working_dir - content (
str
): Text content to write - Returns (
bool
):True
on success,False
otherwise
2. Reading Files
- file_path (
str
): Path to the file, relative to working_dir - Returns (
str
orNone
): File content as string, orNone
if the file does not exist
3. Searching Files
- query (
str
): Substring or regex to match - file_patterns (
List[str]
, optional): Glob patterns for files to search (default:['**/*']
) - Returns (
Dict[str, List[str]]
): Mapping of file paths to lists of matching lines
Unified Example: All File Operations
Below is a complete script that demonstrates how to use all file management commands together. This example is based on the actual usage inreact.py
:
Best Practices
- Use relative paths for portability.
- Check file existence before reading or overwriting.
- Use specific glob patterns in
search_files
for performance. - Process search results programmatically to identify relevant files.
- Handle binary files with encoding/decoding as shown above.
More Advanced Usage
You can combine file operations with thecode()
and code_headless()
methods for AI-powered code editing. See the AI Coding page for details.