FileNotFoundError in Python: Causes and Fixes
FileNotFoundError means Python could not find the file or folder you asked it to use.
This error often appears when you use open(), os.rename(), os.remove(), or other file operations. In most cases, the problem is not Python itself. The problem is the path.
A very common beginner mistake is thinking Python looks for files in the same folder as the script. Sometimes it does, but often Python uses the current working directory instead.
Quick fix
If you want a fast way to avoid this error while debugging, check whether the file exists before opening it:
from pathlib import Path
file_path = Path("data.txt")
if file_path.exists():
with open(file_path, "r", encoding="utf-8") as file:
content = file.read()
print(content)
else:
print(f"File not found: {file_path.resolve()}")
This code:
- creates a path for
data.txt - checks whether that file exists
- reads it only if Python can find it
- prints the full path when debugging
Use the correct path, check that the file really exists, and print the full path when debugging.
What this error means
FileNotFoundError happens when Python cannot find the file or folder you asked for.
This commonly happens with:
open()os.rename()os.remove()- other file and folder operations
Usually, one of these is true:
- the file does not exist
- the path is wrong
- the code is running from a different folder than you expected
- the file name or extension is misspelled
- a folder in the path does not exist
- the file was moved or deleted
When FileNotFoundError happens
You will usually see this error in situations like these:
- Trying to open a file that does not exist
- Using a relative path from the wrong folder
- Misspelling the file name or extension
- Using the wrong slash direction or an invalid path string
- Trying to access a folder or drive that is not available
- Using a path to a file that was moved, renamed, or deleted
Example that causes the error
Here is a simple example:
with open("data.txt", "r", encoding="utf-8") as file:
content = file.read()
print(content)
If data.txt is not in the location Python is searching, you may get an error like this:
Traceback (most recent call last):
File "main.py", line 1, in <module>
with open("data.txt", "r", encoding="utf-8") as file:
FileNotFoundError: [Errno 2] No such file or directory: 'data.txt'
The important part is this:
No such file or directory: 'data.txt'
That tells you the exact path Python failed to find. In this example, Python was looking for a file named data.txt relative to the current working directory.
If you are new to file handling, see how to read a file in Python and Python open() explained.
Fix 1: Check the file name and extension
Start with the simplest check.
Make sure:
- the file name matches exactly
- the extension is correct, such as
.txt,.csv, or.json - there are no extra spaces
- uppercase and lowercase letters match on systems where case matters
For example, these are different names:
data.txtData.txtdata.csvdata.txt
A tiny difference is enough to cause FileNotFoundError.
Fix 2: Use the correct path
A path is the location of a file or folder.
There are two main kinds:
- Relative path: starts from the current working directory
- Absolute path: gives the full location on your computer
Example of a relative path:
with open("files/data.txt", "r", encoding="utf-8") as file:
print(file.read())
Example of an absolute path:
with open("/home/user/project/files/data.txt", "r", encoding="utf-8") as file:
print(file.read())
On Windows, an absolute path may look like this:
with open(r"C:\Users\YourName\Documents\data.txt", "r", encoding="utf-8") as file:
print(file.read())
To see where Python is looking from, print the current working directory:
import os
print(os.getcwd())
Or with pathlib:
from pathlib import Path
print(Path.cwd())
If this is confusing, a full guide to working with file paths in Python can help.
Fix 3: Build paths safely
Typing long paths by hand often causes mistakes.
A better option is to use pathlib.Path or os.path.join().
Using pathlib
from pathlib import Path
file_path = Path("files") / "data.txt"
with open(file_path, "r", encoding="utf-8") as file:
print(file.read())
Using os.path.join()
import os
file_path = os.path.join("files", "data.txt")
with open(file_path, "r", encoding="utf-8") as file:
print(file.read())
These approaches help because they:
- reduce slash mistakes
- make paths easier to read
- make code easier to update later
If you want to learn this style, see os.path.join() explained.
Fix 4: Check that the file exists before using it
If a file may or may not exist, check first.
With pathlib
from pathlib import Path
file_path = Path("data.txt")
if file_path.exists():
print("File exists")
else:
print("File does not exist")
With os.path.exists()
import os
if os.path.exists("data.txt"):
print("File exists")
else:
print("File does not exist")
This is useful, but it does not replace good error handling. A file can still disappear between the check and the time you open it.
A safer pattern is to use try and except when needed:
try:
with open("data.txt", "r", encoding="utf-8") as file:
print(file.read())
except FileNotFoundError:
print("Could not find data.txt")
For more on existence checks, see how to check if a file exists in Python and os.path.exists() explained.
Fix 5: Create the file or folder if needed
Sometimes the file is missing. Sometimes the folder is missing.
If you want to read a file
The file must already exist.
with open("notes.txt", "r", encoding="utf-8") as file:
print(file.read())
If notes.txt is missing, Python raises FileNotFoundError.
If you want to write a new file
open(..., "w") can create a new file if it does not already exist:
with open("notes.txt", "w", encoding="utf-8") as file:
file.write("Hello")
But this only works if the parent folder already exists.
This will fail if the output folder does not exist:
with open("output/notes.txt", "w", encoding="utf-8") as file:
file.write("Hello")
Create the folder first:
from pathlib import Path
folder = Path("output")
folder.mkdir(exist_ok=True)
file_path = folder / "notes.txt"
with open(file_path, "w", encoding="utf-8") as file:
file.write("Hello")
You can also create nested folders:
from pathlib import Path
folder = Path("reports/2026/april")
folder.mkdir(parents=True, exist_ok=True)
file_path = folder / "summary.txt"
with open(file_path, "w", encoding="utf-8") as file:
file.write("Report created")
If you are learning the basics, Python file handling basics: read and write is a good next step.
How to debug step by step
When you get FileNotFoundError, use this checklist:
- Print the exact path variable before opening the file.
- Print the current working directory.
- Check whether the path is relative or absolute.
- Confirm the file exists in that location.
- Try a small test script with a known file.
Useful debugging commands:
import os
print(os.getcwd())
from pathlib import Path
print(Path.cwd())
from pathlib import Path
print(Path("data.txt").exists())
from pathlib import Path
print(Path("data.txt").resolve())
import os
print(os.path.exists("data.txt"))
import os
print(os.listdir())
A simple debugging script:
from pathlib import Path
file_path = Path("data.txt")
print("Current folder:", Path.cwd())
print("Path given:", file_path)
print("Full path:", file_path.resolve())
print("Exists:", file_path.exists())
This helps you see exactly where Python is looking.
Common beginner situations
These situations cause this error very often:
- Running a script from an IDE where the working directory is different
- Using notebook paths that are different from script paths
- Saving the file in one folder but running code from another
- Trying to open a file from the desktop without using the full path
For example, your script may be in one folder, but your IDE may run it from the project root. That means "data.txt" may not point where you think it does.
Related errors to mention
Sometimes the problem is not FileNotFoundError, but a different file-related error.
PermissionError
This happens when the file exists, but Python is not allowed to access it.
See PermissionError in Python: causes and fixes.
IsADirectoryError
This happens when you pass a folder path where Python expects a file path.
NotADirectoryError
This happens when part of the path is expected to be a folder, but it is not.
OSError
This is a broader error for file system and operating system problems.
FAQ
Why do I get FileNotFoundError when the file is in the same folder?
Python may be running from a different working directory. Check os.getcwd() and compare it to the file location.
Can open() create a missing file?
Yes, with modes like "w" or "a". But it cannot create missing folders in the path.
What is the difference between relative and absolute paths?
A relative path starts from the current working directory. An absolute path gives the full location from the root of the system.
Should I use try-except for FileNotFoundError?
Yes, especially when a file may not exist. It lets your program fail gracefully and show a helpful message.
Why does my path work on one computer but not another?
The file location may be different, or the path format may not match the operating system.