python
io_error
ai_generated
true
FileNotFoundError: [Errno 2] No such file or directory
ID: python/filenotfounderror
90%Fix Rate
92%Confidence
380Evidence
2023-01-01First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| 311 | active | — | — | — |
Root Cause
File path does not exist. Common in scripts with hardcoded paths or relative path assumptions.
genericWorkarounds
-
92% success Use pathlib.Path and resolve relative to __file__ or project root
Path(__file__).parent / 'data' / 'config.json'
-
88% success Check path existence before access with Path.exists()
from pathlib import Path; if Path(filepath).exists(): ...
Sources: https://docs.python.org/3/library/pathlib.html#pathlib.Path.exists
Dead Ends
Common approaches that don't work:
-
Create empty file at the path
58% fail
May not contain expected content, causing downstream errors
-
Hardcode absolute path
70% fail
Breaks portability across machines and environments
Error Chain
Preceded by:
Frequently confused with: