You just wrote an amazing script, configured all the logic, and when you run it, the terminal shows the dreaded error: FileNotFoundError. This is one of the most frustrating moments for anyone starting to learn Python. The problem, in most cases, is not in your code itself but in how you are pointing to the file you want to open. Understanding how Python handles the file system is essential for creating robust automations and reliable programs.
Why can’t Python find your files?
The “file not found” error occurs when the Python interpreter tries to access a resource on disk but the address you provided does not correspond to any real location. Think of it like sending a letter: if the address is incomplete or the zip code is wrong, the mail carrier cannot deliver it. In programming, that address is what we call a “path.”
There are two main causes: typos in the file name and confusion between relative and absolute paths. When you run open('data.txt'), Python searches for that file exactly in the folder where the terminal is currently “standing,” not necessarily the folder where your .py script is saved.
Absolute paths vs relative paths
An absolute path is the full address from the root of your operating system, such as C:UsersNameProjectfile.txt on Windows. According to the Python Software Foundation official documentation, absolute paths are precise but make your code hard to share, since other people will not have the same folder structure.
Relative paths depend on the current working directory (CWD). If you are in the project folder, just call the file name. Problems arise when you use a virtual environment or run the script from a parent folder, because the CWD changes and Python gets lost.
Using os.getcwd() to debug path issues
import os
# Find out where Python is looking for files
current_dir = os.getcwd()
print(f"Python is looking for files in: {current_dir}")The best fix: pathlib and __file__
The most robust solution is to build paths relative to the script’s own location using pathlib and __file__. This way, the path is always correct regardless of where the script is called from. For more file manipulation techniques, see working with zip files in Python.
from pathlib import PathHandle the error gracefully with try/except
try:Frequently asked questions
The file exists in my folder but Python still can’t find it. Why?
Your terminal’s working directory is probably not the same as your script’s folder. Add print(os.getcwd()) at the top of your script to check where Python is actually looking, then adjust your path accordingly.
Should I use os.path or pathlib?
Pathlib (available since Python 3.4) is the modern, recommended approach. It is more readable, object-oriented, and handles cross-platform path separators automatically. Use os.path only when maintaining legacy code.
Why do backslashes cause errors on Windows?
In Python strings, is an escape character. A path like "C:Usersname" is read as C: + carriage return + sers + line break + ame. Fix it with raw strings (r"C:Usersname"), forward slashes ("C:/Users/name"), or pathlib.
Most FileNotFoundError occurrences come down to a mismatched working directory. Using Path(__file__).parent to anchor paths to your script’s location is the most reliable pattern for any project.






