📡 You're offline — showing cached content
New version available!
Quick Access
Tutorials AI Agents and Automation File System Agents

File System Agents

5 min read
File system tools give agents read/write access to local files, directories, and databases.

File System and Data Processing Agents

from langchain_core.tools import tool
import os, json, csv

@tool
def list_directory(path: str) -> str:
    """List files in a directory."""
    files = os.listdir(path)
    return json.dumps({"files": files, "count": len(files)})

@tool
def read_file(path: str) -> str:
    """Read contents of a text file (first 5000 chars)."""
    with open(path) as f:
        return f.read(5000)

@tool
def write_file(path: str, content: str) -> str:
    """Write content to a file."""
    with open(path, "w") as f:
        f.write(content)
    return f"Written {len(content)} bytes to {path}"

@tool
def run_sql_query(query: str, db_path: str = "data.db") -> str:
    """Execute a SQLite query and return results."""
    import sqlite3
    conn = sqlite3.connect(db_path)
    cursor = conn.execute(query)
    rows = cursor.fetchall()
    cols = [d[0] for d in cursor.description]
    return json.dumps({"columns":cols,"rows":rows[:50]})

@tool
def analyse_csv(path: str) -> str:
    """Analyse a CSV file and return statistics."""
    import pandas as pd
    df = pd.read_csv(path)
    return df.describe().to_json()