> ## Documentation Index
> Fetch the complete documentation index at: https://crewai-cursor-filewritetool-docs-aa85.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# File Write

> Writes text content to a file.

# `FileWriterTool`

## Description

`FileWriterTool` writes text content to a file. Missing directories are created
automatically. The default text encoding is UTF-8.

Writes are confined to a sandbox directory. By default that directory is the
current working directory. Pass `base_dir` to change it.

## Installation

```shell theme={null}
pip install 'crewai[tools]'
```

## Example

```python Code theme={null}
from crewai_tools import FileWriterTool

file_writer_tool = FileWriterTool()

result = file_writer_tool.run(
    filename='example.txt',
    content='This is a test content.',
    directory='test_directory',
)
print(result)
```

## Arguments

The agent supplies these at runtime:

* `filename`: Name of the file to write, relative to `directory`. May include
  subdirectories. Those subdirectories are created if they do not exist.
* `content`: Text content to write.
* `directory` (optional): Directory to write into. A relative path resolves
  inside the tool's allowed directory (`base_dir` when set, otherwise the
  current working directory) and defaults to that directory's root (`./`).
  Created if it does not exist.
* `overwrite` (optional): Whether to replace an existing file. Accepts a
  boolean or the strings `y`/`yes`/`t`/`true`/`on`/`1` and
  `n`/`no`/`f`/`false`/`off`/`0`. Defaults to `false`. When `false` and the
  file already exists, the tool returns an error and does not modify the file.

You set these when constructing the tool:

* `base_dir` (optional): Directory that writes must stay inside. Defaults to
  the current working directory. A relative path is resolved when the tool is
  constructed, so a later change of working directory does not move the
  sandbox.
* `encoding` (optional): Text encoding used to write the file. Defaults to
  `utf-8`.

## Allowed paths

Runtime `directory` and `filename` values are usually chosen by an LLM. The
tool rejects paths that escape the sandbox:

* The resolved `directory` must be inside `base_dir` (the current working
  directory by default).
* The resolved file path must be inside that `directory`.
* `..` segments, absolute paths, and symlinks are resolved before both checks.

To allow writes under a different tree, set `base_dir`:

```python Code theme={null}
file_writer_tool = FileWriterTool(base_dir='/var/output')
```

<Note>
  Previously, an absolute `directory` could write anywhere the process had
  permission. If you relied on that, set `base_dir` to the tree you want to
  allow. Setting `CREWAI_TOOLS_ALLOW_UNSAFE_PATHS=true` restores the old
  behavior, but it disables path and URL checks process-wide for every
  crewai-tools tool. Prefer `base_dir`.
</Note>
