create parent folder in Editor.create_file

This commit is contained in:
黄伟韬 2024-09-29 14:04:25 +08:00
parent f03f2c43a3
commit 58907d0dbb

View file

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