FoundationAgents/MetaGPT · error · FileExistsError
File '{filename}' already exists.
Error message
File '{filename}' already exists. What it means
Raised by Editor.create_file when the target path already exists (any filesystem entry: file, directory, or symlink). create_file is write-once by design; it will not truncate or overwrite existing content, so it refuses rather than destroying data.
Source
Thrown at metagpt/tools/libs/editor.py:366
self._check_current_file()
with self.current_file.open() as file:
total_lines = max(1, sum(1 for _ in file))
self.current_line = self._clamp(self.current_line - self.window, 1, total_lines)
output = self._cur_file_header(self.current_file, total_lines)
output += self._print_window(self.current_file, self.current_line, self.window)
return output
async def create_file(self, filename: str) -> str:
"""Creates and opens a new file with the given name.
Args:
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.")
await awrite(filename, "\n")
self.open_file(filename)
return f"[File {filename} created.]"
@staticmethod
def _append_impl(lines, content):
"""Internal method to handle appending to a file.
Args:
lines: list[str]: The lines in the original file.
content: str: The content to append to the file.
Returns:
content: str: The new content of the file.
n_added_lines: int: The number of lines added to the file.
"""
content_lines = content.splitlines(keepends=True)View on GitHub (pinned to 11cdf466d0)
Solutions
- Check filename.exists() first and open_file instead when the file already exists
- If regeneration is intended, delete or move the old file before create_file
- Guard retries: only call create_file when the file is absent
Example fix
// before
editor.create_file('src/new_module.py') # FileExistsError
// after
if filename.exists():
editor.open_file(str(filename))
else:
editor.create_file(str(filename)) Defensive patterns
Strategy: validation
Validate before calling
from pathlib import Path
if Path(filename).exists():
editor.open_file(filename)
else:
editor.create_file(filename) Type guard
def should_create(p: str) -> bool:
return not Path(p).exists() Try / catch
try:
editor.create_file(filename)
except FileExistsError:
editor.open_file(filename) Prevention
- Make create-then-open an idempotent helper used by all retries
- Do not retry create_file blindly in agent loops; check existence first
- Treat FileExistsError from create_file as 'already done', not as failure
When it happens
Trigger: editor.create_file('a.py') when a.py already exists on disk; re-running a generation step that already created the file; passing a path that collides with an existing directory.
Common situations: Retry loops in agent workflows re-invoking create_file after a partial failure; scaffolding scripts run twice; idempotency expectations where the tool is deliberately non-idempotent.
Related errors
- File {path} not found
- Could not access or create directories.
- File {file_name} not found.
- Directory {dir_path} not found
- No file open. Use the open_file function first.
AI-assisted analysis of FoundationAgents/MetaGPT@11cdf466d0 (2026-08-14).
Data as JSON: /api/errors/4f0848272949a105.
Report an issue: GitHub.