Python Fix

Python FileNotFoundError Even Though the File Exists

When Python says a file is missing even though you can see it, the program is usually checking a different path than the one you expect.

Difficulty: Easy–Medium Time: 15–35 minutes Updated: July 2026 Works for: Windows / macOS / Linux

FileNotFoundError is precise: the operating system could not find the path Python requested. The confusing part is that the requested path may not be the path you had in mind.

The most common cause is a relative path resolved from the current working directory rather than from the script’s folder. Other causes include hidden file extensions, misspelled names, unescaped Windows backslashes, incorrect capitalization, moved files, and code running under another user or environment.

Step 1 — Print the exact path Python is using

from pathlib import Path

print("Working directory:", Path.cwd())
target = Path("data.csv")
print("Requested path:", target.resolve())
print("Exists:", target.exists())

Compare the resolved path with the actual file location. This single check solves a large percentage of “but the file is right there” cases.

Fix 1 — Build the path from the script directory

from pathlib import Path

base_dir = Path(__file__).resolve().parent
file_path = base_dir / "data.csv"

with file_path.open("r", encoding="utf-8") as file:
    content = file.read()

This is reliable for files shipped beside the script because it does not depend on the terminal or IDE’s working directory.

Fix 2 — Open the project folder in VS Code

Running a loose file can give VS Code a different working directory than expected. Open the full project folder, then use the integrated terminal:

python script.py

If a debugger configuration changes the working directory, inspect launch.json for a custom cwd value.

Fix 3 — Handle Windows backslashes correctly

This path is dangerous because sequences such as \n and \t have special meanings:

file_path = "C:\new\test.txt"

Use a raw string, escaped backslashes, forward slashes, or pathlib:

from pathlib import Path

file_path = Path(r"C:\new\test.txt")
# or
file_path = Path("C:/new/test.txt")

Fix 4 — Check the real filename and extension

Windows may hide known extensions. A file displayed as data.csv could actually be data.csv.txt. Enable file-name extensions in File Explorer and verify the exact name.

from pathlib import Path

for item in Path(".").iterdir():
    print(repr(item.name))

Using repr can expose trailing spaces or unexpected characters.

Fix 5 — Check capitalization on macOS and Linux

Data.csv and data.csv may be different files on case-sensitive systems. Match every character exactly, including the extension.

Fix 6 — Verify parent folders before writing

Opening a file in write mode can create the file, but it cannot create missing parent directories.

from pathlib import Path

output = Path("reports/2026/result.txt")
output.parent.mkdir(parents=True, exist_ok=True)
output.write_text("Complete", encoding="utf-8")

Fix 7 — Resolve paths from user input safely

from pathlib import Path

user_value = input("File path: ").strip().strip('"')
file_path = Path(user_value).expanduser().resolve()

if not file_path.is_file():
    raise FileNotFoundError(f"No file found at: {file_path}")

Stripping surrounding quotes helps when users paste a Windows path copied as a quoted string.

Fix 8 — Check whether another process moved or renamed the file

Downloads, cloud-sync clients, temporary-file cleaners, and export tools may rename or relocate files. Print the parent directory contents immediately before opening the target.

Fix 9 — Distinguish missing files from permission problems

A true permission issue normally raises PermissionError, but network shares, disconnected drives, and sandboxed environments can make resources appear absent. Confirm the drive or mount is available under the same user account running Python.

Avoid hard-coding personal absolute paths into code that will run on another computer. Prefer project-relative paths, configuration values, or user-selected paths.

A practical debugging pattern

from pathlib import Path

path = Path("data/input.csv")

print("cwd:", Path.cwd())
print("raw:", path)
print("absolute:", path.resolve())
print("exists:", path.exists())
print("is_file:", path.is_file())
print("parent exists:", path.parent.resolve().exists())

These checks show exactly where the mismatch occurs instead of guessing.

FAQ

Why does the file open in one script but not another?

The scripts may run with different working directories or construct the path differently.

Should I always use absolute paths?

No. Absolute paths are useful for diagnosis, but pathlib-based project-relative paths are usually more portable.

Why does the code fail only in the VS Code debugger?

The debugger may use a workspace directory or custom cwd different from the terminal where the script works.