File

Read and write files

Overview

File tool allows you to read, write, and manage files from your AI agents.

The user asks to read or write a file; the agent performs the operation and returns the result.

Diagram

FileTool Flow

👤 User

🤖 Agent

🔧 FileTool

✅ Result

Installation

bash
pip install "praisonai[tools]"

Quick Start

  1. Simple Usage
    python
    from praisonai_tools import FileTool
    
    # Initialize
    file = FileTool()
    
    # Read file
    content = file.read("document.txt")
    print(content)
  2. With Configuration

    Use the same tool with an agent — see Usage with Agent below, or pass env vars and options from the sections above.

Usage with Agent

python
from praisonaiagents import Agent
from praisonai_tools import FileTool

agent = Agent(
    name="FileManager",
    instructions="You help manage files.",
    tools=[FileTool()]
)

response = agent.chat("Read the contents of config.json")
print(response)

Available Methods

read(path)

Read file contents.

python
from praisonai_tools import FileTool

file = FileTool()
content = file.read("data.txt")

write(path, content)

Write content to a file.

python
file.write("output.txt", "Hello World!")

list_dir(path)

List directory contents.

python
files = file.list_dir("./documents")

Common Errors

ErrorCauseSolution
File not foundInvalid pathCheck file path
Permission deniedNo accessCheck permissions

How It Works

Diagram
File systemToolAgentUserFile systemToolAgentUserRequestCall toolQueryDataResultResponse

Best Practices

Use absolute paths

Pass absolute paths so file operations are predictable regardless of the working directory.

Confirm before overwriting

Check for existing files before write to avoid data loss.

Restrict to a safe directory

Limit file operations to a known workspace so the agent can’t touch sensitive paths.


Related Tools