Implementation:CrewAIInc CrewAI File Writer Tool
| Knowledge Sources | |
|---|---|
| Domains | Tools, File_System |
| Last Updated | 2026-02-11 00:00 GMT |
Overview
Writes content to files with automatic directory creation and configurable overwrite protection.
Description
FileWriterTool extends BaseTool to provide file writing capabilities using FileWriterToolInput schema with parameters for filename, directory (defaults to ./), overwrite (boolean or string), and content. The _run() method creates directories if they do not exist using os.makedirs, converts overwrite strings to booleans via a strtobool helper function, checks for existing files when overwrite is disabled, and writes content using appropriate file modes (w for overwrite, x for exclusive creation). Error handling covers FileExistsError, KeyError, and general exceptions.
Usage
Use this tool when a CrewAI agent needs to persist output to files, such as generating reports, saving logs, creating configuration files, or writing analysis results to disk.
Code Reference
Source Location
- Repository: CrewAI
- File:
lib/crewai-tools/src/crewai_tools/tools/file_writer_tool/file_writer_tool.py - Lines: 1-59
Signature
class FileWriterTool(BaseTool):
name: str = "File Writer Tool"
description: str = "A tool to write content to a specified file. ..."
args_schema: type[BaseModel] = FileWriterToolInput
def _run(self, **kwargs: Any) -> str: ...
Import
from crewai_tools import FileWriterTool
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| filename | str | Yes | Name of the file to write |
| content | str | Yes | Content to write to the file |
| directory | str | No | Directory path (default: ./)
|
| overwrite | str or bool | No | Whether to overwrite an existing file (default: False)
|
Outputs
| Name | Type | Description |
|---|---|---|
| _run() returns | str | Success message with file path, or an error message if the operation fails |
Usage Examples
Basic Usage
from crewai_tools import FileWriterTool
tool = FileWriterTool()
result = tool.run(
filename="report.txt",
content="Analysis results...",
directory="./output",
)
Overwrite Existing File
from crewai_tools import FileWriterTool
tool = FileWriterTool()
result = tool.run(
filename="report.txt",
content="Updated analysis...",
directory="./output",
overwrite=True,
)