FileNotFoundError vs PermissionError Explained
These two Python errors look similar because both happen when you work with files or folders.
But they mean different things:
FileNotFoundErrormeans Python cannot find the path you gave.PermissionErrormeans Python found the path, but it is not allowed to access it.
This page helps you tell them apart quickly, understand why each one happens, and choose the right fix.
Quick fix
Use this pattern to separate a missing file problem from a permission problem:
from pathlib import Path
path = Path("data.txt")
if not path.exists():
print("The file path is wrong or the file does not exist.")
else:
try:
with open(path, "r", encoding="utf-8") as file:
print(file.read())
except PermissionError:
print("The file exists, but Python does not have permission to open it.")
Use this approach before trying other fixes.
It answers the first important question:
Does the path exist at all?
What is the difference?
FileNotFoundErrormeans Python cannot find the file or folder at the path you gave.PermissionErrormeans the file or folder exists, but Python is not allowed to access it in the way you requested.- Both are file system errors, but they need different fixes.
- The fastest check is: does the path exist?
A simple way to think about it:
- Missing path →
FileNotFoundError - Blocked access →
PermissionError
If you are not sure how file paths work, see working with file paths in Python.
When FileNotFoundError happens
FileNotFoundError usually happens when Python looks for a path and cannot find it.
Common reasons:
- The filename is misspelled.
- The file is in a different folder.
- You used a relative path from the wrong working directory.
- A parent directory in the path does not exist.
- You are trying to open a file before it is created.
For example, if your script says open("data.txt"), Python looks for data.txt relative to the current working directory, not always the folder where your script is saved.
If you need to check that, see os.getcwd() explained.
When PermissionError happens
PermissionError happens when the path exists, but Python cannot use it the way you asked.
Common reasons:
- The file exists, but your user account cannot read or write it.
- You tried to write to a read-only file or folder.
- You opened a folder as if it were a file.
- Another program or system rule is blocking access.
- You are working in a protected location such as some system folders.
For example, trying to save a file in a protected system directory may fail even if that directory is real and spelled correctly.
Example: FileNotFoundError
Here is a simple example:
with open("missing.txt", "r", encoding="utf-8") as file:
print(file.read())
Typical result:
FileNotFoundError: [Errno 2] No such file or directory: 'missing.txt'
Python fails before it can read anything because the file is not there.
How to fix it
Check these things:
- Is the filename correct?
- Is the extension correct, such as
.txtvs.csv? - Is the file in the folder you think it is?
- Are you running the script from a different directory?
This small debugging example can help:
import os
from pathlib import Path
filename = "missing.txt"
print("Current working directory:", os.getcwd())
print("Absolute path:", Path(filename).resolve())
print("Exists:", os.path.exists(filename))
If you want a full guide for this error, see FileNotFoundError: No such file or directory fix.
Example: PermissionError
Here is a simple example that may raise PermissionError on some systems because the location is protected:
with open("/root/example.txt", "w", encoding="utf-8") as file:
file.write("Hello")
Possible result:
PermissionError: [Errno 13] Permission denied: '/root/example.txt'
In this case, the problem is not that /root is missing.
The problem is that your Python program is not allowed to write there.
How to fix it
Possible fixes:
- Save the file somewhere you can access, such as your project folder.
- Change the file path.
- Change file or folder permissions if appropriate.
- Close another program that may be locking the file.
- Make sure you are using the correct file mode.
If you need a full error-specific guide, see PermissionError: Permission denied fix.
How to debug step by step
When you are not sure which error you are dealing with, go in this order.
1. Print the exact path
Make sure your code is using the path you expect.
from pathlib import Path
path = Path("data.txt")
print(path)
print(path.resolve())
2. Check the current working directory
Relative paths depend on where Python is running.
import os
print(os.getcwd())
3. Check whether the path exists
import os
print(os.path.exists("data.txt"))
You can also use pathlib:
from pathlib import Path
print(Path("data.txt").exists())
For more on this, see how to check if a file exists in Python and os.path.exists() explained.
4. Check whether it is a file or a directory
Sometimes the path exists, but it points to a folder instead of a file.
import os
print(os.path.isfile("data.txt"))
print(os.path.isdir("data.txt"))
If you pass a directory to open(), you may get a different error such as IsADirectoryError: Is a directory fix.
5. Try a simpler location
Try using a file in the same folder as your script or project.
This removes many path and permission problems.
6. Catch the errors separately
This gives clearer messages and makes debugging easier.
from pathlib import Path
path = Path("data.txt")
try:
with open(path, "r", encoding="utf-8") as file:
print(file.read())
except FileNotFoundError:
print("Python cannot find the file. Check the path and working directory.")
except PermissionError:
print("The file exists, but Python does not have permission to open it.")
How to fix the right error
Do not treat both errors as the same problem.
Fixing FileNotFoundError
Use these fixes when the path does not exist:
- Correct the path
- Correct the filename or extension
- Create the file first
- Create missing parent folders
- Use an absolute path if needed
Fixing PermissionError
Use these fixes when the path exists but access is blocked:
- Choose a file or folder you can access
- Close programs that may be locking the file
- Change file permissions if appropriate
- Use the correct mode, such as read vs write
- Avoid protected system locations unless necessary
If you are unsure, use this rule:
- Check path existence first
- Handle permissions second
Common beginner confusion
These mistakes are very common:
- A wrong folder is not a permission problem.
- An existing file can still fail to open because of permissions.
- A directory path passed to
open()can raise a different error. - On Windows, using raw strings like
r"C:\files\data.txt"or forward slashes can help avoid path mistakes.
Common causes include:
- Typing the wrong filename or extension
- Running the script from a different folder than expected
- Using a relative path that points to the wrong location
- Trying to save to a protected directory
- Trying to read or write a file without permission
- Confusing a file path with a folder path
Useful debugging commands:
import os; print(os.getcwd())
import os; print(os.path.exists('data.txt'))
import os; print(os.path.isfile('data.txt'))
import os; print(os.path.isdir('data.txt'))
from pathlib import Path; print(Path('data.txt').resolve())
FAQ
How do I know if it is FileNotFoundError or PermissionError?
First check whether the path exists. If it does not exist, it is usually FileNotFoundError. If it exists but access is blocked, it is usually PermissionError.
Can a file exist and still raise an error?
Yes. A file can exist but still raise PermissionError if Python cannot read or write it.
Should I catch both errors?
Yes, if your code works with files. Catching them separately gives clearer messages and helps you apply the correct fix.
Is a bad relative path the same as missing file?
Yes in practice. Python only sees that the file cannot be found from the current working directory.