FoundationAgents/MetaGPT · error · FileNotFoundError
File {path} not found
Error message
File {path} not found What it means
Raised by Editor.open_file after path normalization (_try_fix_path) when the resolved path does not point to an existing regular file. The editor deliberately fails fast instead of creating the file: open_file is strictly for existing files, while create_file makes new ones.
Source
Thrown at metagpt/tools/libs/editor.py:297
def open_file(
self, path: Union[Path, str], line_number: Optional[int] = 1, context_lines: Optional[int] = None
) -> str:
"""Opens the file at the given path in the editor. If line_number is provided, the window will be moved to include that line.
It only shows the first 100 lines by default! Max `context_lines` supported is 2000, use `scroll up/down`
to view the file if you want to see more.
Args:
path: str: The path to the file to open, preferred absolute path.
line_number: int | None = 1: The line number to move to. Defaults to 1.
context_lines: int | None = 100: Only shows this number of lines in the context window (usually from line 1), with line_number as the center (if possible). Defaults to 100.
"""
if context_lines is None:
context_lines = self.window
path = self._try_fix_path(path)
if not path.is_file():
raise FileNotFoundError(f"File {path} not found")
self.current_file = path
with path.open() as file:
total_lines = max(1, sum(1 for _ in file))
if not isinstance(line_number, int) or line_number < 1 or line_number > total_lines:
raise ValueError(f"Line number must be between 1 and {total_lines}")
self.current_line = line_number
# Override WINDOW with context_lines
if context_lines is None or context_lines < 1:
context_lines = self.window
output = self._cur_file_header(path, total_lines)
output += self._print_window(path, self.current_line, self._clamp(context_lines, 1, 2000))
self.resource.report(path, "path")
return output
View on GitHub (pinned to 11cdf466d0)
Solutions
- Verify the path exists with Path(p).is_file() before calling open_file
- If the file is new, use editor.create_file(path) instead of open_file
- Pass an absolute path, or check what base directory _try_fix_path resolves relative paths against
Example fix
// before
editor.open_file('src/utils/helper.py') # FileNotFoundError
// after
p = Path('src/utils/helper.py')
if p.is_file():
editor.open_file(str(p))
else:
editor.create_file(str(p)) Defensive patterns
Strategy: validation
Validate before calling
from pathlib import Path
p = Path(path)
if not p.is_file():
raise SystemExit(f'missing file: {p.resolve()}')
editor.open_file(str(p)) Type guard
def is_openable_file(p: str) -> bool:
return Path(p).is_file() Try / catch
try:
editor.open_file(path)
except FileNotFoundError:
editor.create_file(path) # only if creating is intended Prevention
- Prefer absolute paths so resolution does not depend on CWD
- Use create_file for new files and open_file for existing ones
- Validate paths at the boundary where LLM/user input enters your pipeline
When it happens
Trigger: editor.open_file('src/app.py') where the file does not exist; passing a directory path; passing a relative path that _try_fix_path resolves against the wrong base directory; typos or wrong-case filenames on case-sensitive filesystems.
Common situations: Relative paths resolved from an unexpected CWD; agent-generated paths that guess at filenames; files not yet created because a previous create/write step failed; symlink targets deleted.
Related errors
- Directory {dir_path} not found
- File '{filename}' already exists.
- Invalid path or file name.
- Could not access or create directories.
- File {file_name} not found.
AI-assisted analysis of FoundationAgents/MetaGPT@11cdf466d0 (2026-08-14).
Data as JSON: /api/errors/b884a20e72a00494.
Report an issue: GitHub.