From 58907d0dbb75b397578bcfa57358411185b73ca5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E9=BB=84=E4=BC=9F=E9=9F=AC?= Date: Sun, 29 Sep 2024 14:04:25 +0800 Subject: [PATCH] create parent folder in Editor.create_file --- metagpt/tools/libs/editor.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/metagpt/tools/libs/editor.py b/metagpt/tools/libs/editor.py index ac92f31b6..c49d33a6e 100644 --- a/metagpt/tools/libs/editor.py +++ b/metagpt/tools/libs/editor.py @@ -352,13 +352,14 @@ class Editor(BaseModel): """Creates and opens a new file with the given name. Args: - filename: str: The name of the file to create. + filename: str: The name of the file to create. If the parent directory does not exist, it will be created. """ filename = self._try_fix_path(filename) if filename.exists(): raise FileExistsError(f"File '{filename}' already exists.") - + if not filename.parent.exists(): + os.makedirs(filename.parent, exist_ok=True) with filename.open("w") as file: file.write("\n")