If you’ve ever seen the error message FileNotFoundError: [Errno 2] No such file or directory
, don’t worry — you’re not alone. This is one of the most common file-related errors in Python, especially when working with file reading or writing. The error simply means Python can’t find the file you’re asking it to open.
Fortunately, it’s usually easy to fix once you understand what’s causing it.
What Causes This Error?
Here are a few typical scenarios where this error occurs:
- Incorrect File Path: The path to the file is misspelled or incorrect.
- File Doesn’t Exist: You’re trying to open a file that hasn’t been created yet.
- Wrong Working Directory: Your script is running from a different folder than you expect.
- Relative vs. Absolute Paths Confusion: You’re using a relative path, but it’s resolving in an unexpected way.
Each of these issues can prevent Python from locating the file you’re referencing.
How to Fix It
1. Double-check the file path
Make sure the file name and path are exactly correct.
file = open("data/report.txt", "r")
If data/report.txt
doesn’t exist or is misspelled, you’ll get the error.
Why it works: Ensuring the correct path tells Python exactly where the file is.
2. Use absolute paths
Instead of relying on where the script runs, specify the full path.
file = open("/Users/jane/Documents/report.txt", "r")
Why it works: Absolute paths don’t depend on where the script is executed from.
3. Check if the file exists before opening
You can use the os
module to avoid the error.
import os
if os.path.exists("report.txt"):
with open("report.txt", "r") as file:
content = file.read()
else:
print("File not found.")
Why it works: It prevents your program from crashing by checking first.
4. Create the file if it doesn’t exist
If you’re writing to a file and it doesn’t exist, Python will create it in write (w
) or append (a
) mode.
with open("newfile.txt", "w") as file:
file.write("This is a new file.")
Why it works: w
mode creates a file if it doesn’t already exist.
Real Example
Problematic Script:
with open("notes/todo.txt", "r") as f:
tasks = f.readlines()
If the notes
folder or todo.txt
file doesn’t exist, this will raise the FileNotFoundError
.
Fixed Script:
import os
file_path = "notes/todo.txt"
if os.path.exists(file_path):
with open(file_path, "r") as f:
tasks = f.readlines()
else:
print("The file you're trying to open does not exist.")
This approach helps the program respond gracefully instead of crashing.
Tips to Prevent This Error
- Use
os.path.exists()
to verify the file before opening. - Log or print the working directory (
os.getcwd()
) to confirm paths. - Organize files consistently and document expected file locations.
Conclusion
The FileNotFoundError
might seem frustrating, but it’s easy to fix with a bit of care and planning. Make sure your file paths are correct and your code accounts for missing files. Keep experimenting and remember — even small errors are great learning opportunities. Save this post for future reference!