From 4416aa1aa1c0dba9e37bf6a2694e28aa6bbedf88 Mon Sep 17 00:00:00 2001 From: yousefissa Date: Mon, 3 Apr 2023 08:48:43 -0700 Subject: [PATCH 1/2] create file dir if it doesnt exist during write_to_file --- scripts/file_operations.py | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) 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): From 9ef4fab084633e4289226a6dd059a598085ec876 Mon Sep 17 00:00:00 2001 From: yousefissa Date: Mon, 3 Apr 2023 08:50:07 -0700 Subject: [PATCH 2/2] error handling back --- scripts/file_operations.py | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/scripts/file_operations.py b/scripts/file_operations.py index d7c7a1b082..81ad471577 100644 --- a/scripts/file_operations.py +++ b/scripts/file_operations.py @@ -29,14 +29,16 @@ def read_file(filename): def write_to_file(filename, text): - 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." - + try: + 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." + except Exception as e: + return "Error: " + str(e) def append_to_file(filename, text):