← Back

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

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

  1. Print os.getcwd() on the line before open(). Compare it with the folder you assume. This one check resolves most cases.
  2. 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).
  3. List the directory with Path('.').iterdir() and read the real names instead of trusting your memory of them.
  4. Check path.exists() before opening when a missing file is a legitimate outcome, and branch — do not wrap a big block in a bare except.
  5. 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.

🇮🇳 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.

Python file handling Hindi में पढ़ें →