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

FileAppendTool

Appends the provided contents to a file, creates if it doesn't exist.

Parameters

  • file_path (str): The path to the file to be appended to.
  • content (str): The content to be appended 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, FileWriteTool)

import asyncio
from embedia.tools import FileWriteTool, FileAppendTool, FileReadTool
 
file_writer = FileWriteTool()
file_append = FileAppendTool()
file_reader = FileReadTool()
asyncio.run(file_writer(file_path="test.txt", content="Hello World!"))
asyncio.run(file_append(file_path="test.txt", content=", How are you?"))
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-04T03:57:56.035655+00:00] [id: 140617711825296] [event: Tool Start]
Tool: FileWriteTool
Args: ()
Kwargs: {'file_path': 'test.txt', 'content': 'Hello World!'}
 
[time: 2023-10-04T03:57:56.035989+00:00] [id: 140617711825296] [event: Tool End]
Tool: FileWriteTool
ExitCode: 0
Output:
12
 
[time: 2023-10-04T03:57:56.037013+00:00] [id: 140617687690576] [event: Tool Start]
Tool: FileAppendTool
Args: ()
Kwargs: {'file_path': 'test.txt', 'content': ', How are you?'}
 
[time: 2023-10-04T03:57:56.037155+00:00] [id: 140617687690576] [event: Tool End]
Tool: FileAppendTool
ExitCode: 0
Output:
14
 
[time: 2023-10-04T03:57:56.037782+00:00] [id: 140617712625296] [event: Tool Start]
Tool: FileReadTool
Args: ()
Kwargs: {'file_path': 'test.txt'}
 
[time: 2023-10-04T03:57:56.037860+00:00] [id: 140617712625296] [event: Tool End]
Tool: FileReadTool
ExitCode: 0
Output:
Hello World!, How are you?
Contents of text.txt: Hello World!, How are you?