File Management
How to create, read, and search files using the Local SDK (with unified example)
The Local SDK provides a set of methods for managing files in your project directory. This guide explains how to use these methods, referencing the actual implementation in our LangGraph React App. At the end, you’ll find a unified example script that demonstrates all file operations together.
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.
All file paths are relative to your SDK’s working directory. The SDK will automatically create parent directories if they don’t exist.
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 in react.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 the code()
and code_headless()
methods for AI-powered code editing. See the AI Coding page for details.