Home › Errors › FileNotFoundError
FileNotFoundError: the path is relative to where you ran it
Exception: FileNotFoundErrorCategory: runtime (I/O)Raised at: the open() call, before any read or write happens
What this error means
open() looked at the path you gave it, resolved it against the current working directory, and found nothing there. Nothing was read, nothing was written — the call failed before touching the filesystem.
The number in the message is the operating-system error code: [Errno 2] is POSIX ENOENT, 'no such entry'. On Windows the same situation can read [WinError 3] The system cannot find the path specified when a folder in the path is missing, and [WinError 2] when the file itself is.
Cause 1 — the working directory is not the script's folder
This is the cause behind most reports. A relative path like "data.csv" means os.getcwd() + "/data.csv", and os.getcwd() is wherever the process was launched from — not where the .py file lives. Run the same script from your project root and it works; run it from a parent folder, a cron job, a VS Code task or a different terminal and it breaks.
import os
from pathlib import Path
print(os.getcwd()) # where relative paths start
HERE = Path(__file__).resolve().parent # where THIS file lives
path = HERE / "data.csv" # stable no matter the cwd
with path.open(encoding="utf-8") as f:
rows = f.read()
Building the path from file is the fix to adopt permanently: it makes the script work from any directory, in any scheduler, on any machine.
Cause 2 — the name is not what you think
- A hidden extension. Windows and macOS hide known extensions by default, so
datain Finder may really bedata.csv, andnotes.txtmay really benotes.txt.txt. - Case sensitivity.
Data.csvanddata.csvare the same file on Windows and macOS but different files on Linux — code that works on your laptop can fail the moment it deploys. - A trailing space or a Unicode lookalike introduced by copy-pasting the filename from a document or a chat message.
- A directory passed where a file belongs — that raises
IsADirectoryError, but the reverse (a file where a folder belongs) shows up as Errno 2 orNotADirectoryError. - A URL handed to
open().open("https://example.com/data.csv")never fetches anything; userequests.get(url).textorurllib.request.urlopen(url).
from pathlib import Path
p = Path("data.csv")
print(repr(p.name), p.exists(), p.is_file())
print([f.name for f in Path(".").iterdir()]) # what is actually here
Cause 3 — writing into a folder that does not exist
Opening for write creates the file, but not its parents. open("out/2026/report.csv", "w") fails with Errno 2 until out/2026/ exists:
from pathlib import Path
out = Path("out/2026/report.csv")
out.parent.mkdir(parents=True, exist_ok=True)
out.write_text("id,name\n1,Asha\n", encoding="utf-8")
How to debug it
- Print
os.getcwd()on the line beforeopen(). Compare it with the folder you assume. This one check resolves most cases. - Print
repr(path)— quotes expose a stray space, a doubled extension or a backslash that Python ate as an escape ("C:\new\data.csv"contains a newline; use a raw string or forward slashes). - List the directory with
Path('.').iterdir()and read the real names instead of trusting your memory of them. - Check
path.exists()before opening when a missing file is a legitimate outcome, and branch — do not wrap a big block in a bareexcept. - In a container or CI job, remember the working directory is usually
/app, not your project folder. Mount or copy the data to the path the code expects.
Related pages
FileNotFoundError is an OSError, so except OSError catches it along with PermissionError and IsADirectoryError — catch the specific class when you mean to handle only a missing file.
- Python file handling (Hindi) — with open(), read, write, append और missing-file errors — पूरा Hindi guide.
- ValueError: invalid literal for int() — the next error in line once the file opens but a row is not a number.
- The full Python errors guide — every common traceback in one place.
🇮🇳 Hindi में समझें
FileNotFoundError: [Errno 2] No such file or directory ka sabse bada karan hai working directory. Relative path ("data.csv") us folder se shuru hota hai jahan se script run ki gayi — jahan .py file rakhi hai wahan se nahi. Isliye terminal, cron ya VS Code task se chalane par wahi code toot jata hai. Pakka fix: Path(file).resolve().parent / "data.csv" — ab path hamesha file ke folder se banega. Doosri wajah: Windows/Mac extensions chhupa dete hain (data asal me data.csv hai), Linux par case matter karta hai, aur open() me URL daalne se download nahi hota. Debug karne ke liye print(os.getcwd()) aur print(repr(path)) lagao.