We use cookies on our website.
Docs
API Reference
Tools
FileWriteTool

FileWriteTool

Writes the provided contents to a file, overwrites if it.

Parameters

  • file_path (str): The path to the file to be written to.
  • content (str): The content to be written to the file.
  • encoding (str, optional): The encoding of the file, defaults to utf-8.

Returns

The return value is of the type: ToolReturn (Learn more about: ToolReturn) with the following output and exit_code:

  • output (int): The number of characters written to the file.
  • exit_code (int): 0 if success, 1 if failure.

Usage

(Learn more about: FileReadTool)

import asyncio
from embedia.tools import FileWriteTool, FileReadTool
 
file_writer = FileWriteTool()
file_reader = FileReadTool()
resp = asyncio.run(file_writer(file_path="test.txt", content="Hello World!"))
print("Number of characters written:", resp.output)
resp = asyncio.run(file_reader(file_path="test.txt"))
print("Contents of text.txt:", resp.output)

Running the above code prints the following:

[time: 2023-10-03T09:48:48.658832+00:00] [id: 139987675802832] [event: Tool Start]
Tool: FileWriteTool
Args: ()
Kwargs: {'file_path': 'test.txt', 'content': 'Hello World!'}
 
[time: 2023-10-03T09:48:48.659225+00:00] [id: 139987675802832] [event: Tool End]
Tool: FileWriteTool
ExitCode: 0
Output:
12
Number of characters written: 12
 
[time: 2023-10-03T09:48:48.660316+00:00] [id: 139987675793680] [event: Tool Start]
Tool: FileReadTool
Args: ()
Kwargs: {'file_path': 'test.txt'}
 
[time: 2023-10-03T09:48:48.660413+00:00] [id: 139987675793680] [event: Tool End]
Tool: FileReadTool
ExitCode: 0
Output:
Hello World!
Contents of text.txt: Hello World!