FileNotFoundError in Python — What It Means and How to Fix It
What Python's FileNotFoundError exception means and how to resolve it.
Published October 27, 2026
FileNotFoundError is raised when you open a file or directory that does not exist at the given path.
What it means and why it happens
Typical causes: wrong path, relative path resolved from a different working directory, or typos.
Read the full message: it names the file and line where the problem was detected, which is the best place to start.
What you see
run the code
FileNotFoundError: [Errno 2] No such file or directory: 'data.csv'
Practical examples
Code that triggers it
open('data.csv')The fix
from pathlib import Path
p = Path(__file__).parent / 'data.csv'
if p.exists():
text = p.read_text(encoding='utf-8')Handle it
try:
open('data.csv').close()
except FileNotFoundError:
print('file missing')Common mistakes
- Relying on the current working directory.
- Windows backslashes in strings.
- Forgetting file extensions.
Key takeaways
- FileNotFoundError: FileNotFoundError is raised when you open a file or directory that does not exist at the given path.
- Read the message and the line number first.
- Fix the cause, don't just silence the error.
FAQ
Where is my working directory?
import os; print(os.getcwd()).
How do I find which line caused FileNotFoundError?
The error message and stack trace show the file and line; start there and work backwards.