Skip to content

AutoGPT is the vision of accessible AI for everyone, to use and to build on. Our mission is to provide the tools, so that you can focus on what matters.

create file dir if it doesnt exist during write_to_file

yousefissa4416aa1aa1c0
diff --git a/scripts/file_operations.py b/scripts/file_operations.py
index 62b3dc4b7d..d7c7a1b082 100644
--- a/scripts/file_operations.py
+++ b/scripts/file_operations.py
@@ -29,13 +29,14 @@ def read_file(filename):
def write_to_file(filename, text):
- try:
- filepath = safe_join(working_directory, filename)
- with open(filepath, "w") as f:
- f.write(text)
- return "File written to successfully."
- except Exception as e:
- return "Error: " + str(e)
+ filepath = safe_join(working_directory, filename)
+ directory = os.path.dirname(filepath)
+ if not os.path.exists(directory):
+ os.makedirs(directory)
+ with open(filepath, "w") as f:
+ f.write(text)
+ return "File written to successfully."
+
def append_to_file(filename, text):