create file dir if it doesnt exist during write_to_file

This commit is contained in:
yousefissa
2023-04-03 08:48:43 -07:00
parent 8cf4bf9278
commit 4416aa1aa1

View File

@@ -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):