FileNotFoundError
Python Programming Language
Severity: ModerateWhat Does This Error Mean?
A FileNotFoundError means Python tried to open or access a file, but could not find it at the path you specified. Either the file does not exist, the path is wrong, or Python is looking in a different folder than you think. This error always happens when working with files — opening, reading, writing, or deleting them.
Affected Models
- Python 3.x
- Python 2.x (as IOError)
Common Causes
- A typo in the file name or path — even one wrong letter means the file cannot be found
- Python is running from a different directory than where the file is located
- The file was moved, renamed, or deleted since the code was written
- Using backslashes in paths on Windows without escaping them (use forward slashes or raw strings instead)
- Forgetting to include the file extension in the name (like writing 'data' instead of 'data.csv')
How to Fix It
-
Check if the file actually exists at the path you specified. Open your file explorer and navigate to the folder to confirm.
A common mistake is writing the path you expect the file to be at, not where it actually is.
-
Print the current working directory with: import os; print(os.getcwd()). This shows you the folder Python is currently running from.
If your file is in a different folder, either move the file or use the full path (like C:/Users/name/Documents/myfile.txt).
-
On Windows, use forward slashes (/) in paths instead of backslashes (\). Python understands both, but backslashes can cause issues if not doubled.
Safe options: 'C:/Users/name/file.txt' or r'C:\Users\name\file.txt' (note the r before the quote, making it a raw string).
-
Check that you included the file extension. If the file is called 'data.csv', your code must say 'data.csv' — not just 'data'.
Windows hides file extensions by default. Enable 'show file extensions' in Windows Explorer to see the full name.
-
Before opening a file, check if it exists: use os.path.exists('your_file.txt') which returns True or False. Then you can handle the missing case gracefully.
Example: if os.path.exists('data.csv'): open it. else: print a friendly error message.
When to Call a Professional
FileNotFoundErrors are always something you can fix yourself. Double-check the file path and make sure the file actually exists in that location. Use os.path.exists() to check if a file exists before trying to open it.
Frequently Asked Questions
What is the difference between a relative path and an absolute path?
A relative path is based on the current folder Python is running from — for example, 'data/file.csv'. An absolute path is the full location starting from the root — for example, 'C:/Users/name/data/file.csv'. Relative paths can fail if Python is running from a different folder. Absolute paths always work, regardless of where Python is started.
How do I find out what folder Python is running from?
Run this in your code: import os; print(os.getcwd()) The output is the current working directory — the folder Python is treating as home base. File paths without a full location are looked up starting from this folder.
Can I open a file in Python without knowing the exact path?
Yes — you can use os.path.join() to build paths in a way that works on any operating system. You can also use pathlib (available in Python 3.4+) which makes file paths much easier to work with. For example: from pathlib import Path; p = Path('folder/file.txt')