FileNotFoundError: [Errno 2] No such file or directory (tested)
FileNotFoundError: [Errno 2] No such file or directory when the file is clearly there almost always means the path is being resolved from somewhere else. We ran one script from two directories on Python 3.13.5 and got opposite results.
1. Relative paths follow where you run from, not where the script is
Layout: project/data.csv and project/scripts/load.py:
import os
from pathlib import Path
print("cwd:", os.getcwd())
for name in ["data.csv", "../data.csv", "data.csv ", "~/data.csv"]:
try:
open(name)
print(repr(name), "ok")
except FileNotFoundError as e:
print(repr(name), "->", e)
HERE = Path(__file__).resolve().parent
print((HERE.parent / "data.csv").read_text().strip())
print(os.path.expanduser("~/data.csv").startswith("/home/"))
Run from project/:
cd project && python3 scripts/load.py
cwd: /tmp/fnf2/project
'data.csv' ok
'../data.csv' -> [Errno 2] No such file or directory: '../data.csv'
'data.csv ' -> [Errno 2] No such file or directory: 'data.csv '
'~/data.csv' -> [Errno 2] No such file or directory: '~/data.csv'
a,b
True
Run from project/scripts/:
cd project/scripts && python3 load.py
cwd: /tmp/fnf2/project/scripts
'data.csv' -> [Errno 2] No such file or directory: 'data.csv'
'../data.csv' ok
'data.csv ' -> [Errno 2] No such file or directory: 'data.csv '
'~/data.csv' -> [Errno 2] No such file or directory: '~/data.csv'
a,b
True
Measured: the same script, the same file on disk, opposite results: open("data.csv") resolves against the current working directory (os.getcwd()), which is wherever the command was run from, or whatever your IDE, cron or a service manager sets. That's the usual “FileNotFoundError but the file exists”. The path built from Path(__file__).resolve().parent worked in both runs.
2. Names that look right but aren't
Measured in both runs above: 'data.csv ' (trailing space, common when the name comes from input or a config file) and '~/data.csv' both fail. open() does not expand ~; that's the shell's job. Use os.path.expanduser() or Path(...).expanduser(), and .strip() names you read from text. The error shows the path in quotes, so look closely at it.
3. Fix checklist
- Print
os.getcwd()and the absolute path:Path(name).resolve(). - For files that ship with your code, build paths from
Path(__file__).resolve().parent. - For user-supplied paths:
Path(name.strip()).expanduser(). - Windows only (not tested here): use raw strings or forward slashes, because
"C:\new\data.csv"contains a\nnewline escape.
Frequently asked questions
Why do I get FileNotFoundError when the file exists?
Relative paths are resolved against the current working directory, not the script's folder. We measured the same script finding data.csv when run from project/ and failing when run from project/scripts/.
How do I open a file relative to the script in Python?
Path(__file__).resolve().parent / 'data.csv'. We measured it working regardless of where the script was run from.
Does Python's open() understand ~?
No. We measured open('~/data.csv') raising Errno 2; use os.path.expanduser() first.
What does [Errno 2] No such file or directory mean?
Nothing exists at the exact path given, after resolving it against the current directory. Check the quoted path in the message for spaces, ~ and the working directory.
Written with AI assistance. Measured on 2026-10-09 with Python 3.13.5 on Linux: one script opening data.csv, ../data.csv, a name with a trailing space and ~/data.csv, run from two different working directories, plus the Path(__file__) fix. Every block was run verbatim and its output pasted from that run. Windows paths were not tested. PlainHeaders is our extension; everything else here is built-in Python.