FileNotFoundError

Python SystemOS ErrorCommonLast updated: June 29, 2026Tested on:CPython v3.12PIP v24.0June 2026

This error occurs when you attempt to open or access a file path that does not exist in the file system.

FileNotFoundError Quick Fix⏱️ Est. Fix Time: 2 minutes

Usually happens because:

  • Executing the command from a different folder than the relative path base
  • Spelling typo or casing discrepancy in the file or folder path
  • Parent directory folders have not been created

🔍 Quick Checklist:

What is FileNotFoundError?

The 'FileNotFoundError' is raised when a file system operation (like opening a file with 'open()', listing a directory, or deleting a file) references a path that does not exist. This is the Python equivalent of POSIX errno ENOENT. Typical causes include typing spelling errors, relative path confusion where the program runs from a different working directory than the target file, or trying to write to a nested folder path that has not been created yet.

Common Causes

  • Wrong relative path context: The executing terminal working directory does not match the script's assumed relative path location.
  • Spelling typo in file name or directory: A simple typo or case mismatch in the file path string.
  • Missing parent directory structure: Attempting to create a file in a subdirectory that has not been initialized (e.g. saving to 'logs/app.log' when 'logs/' does not exist).
CauseFrequency
Running script from a different working directory (relative path issue)⭐⭐⭐⭐⭐
Spelling typo or casing mismatch in path string⭐⭐⭐⭐
Missing parent directory folders hierarchy⭐⭐⭐

Common Mistakes

  • Running scripts from a cron tab task scheduler or systemd service using relative paths, which fails because their active directory context defaults to root namespaces.
  • Using Windows backslashes (`\`) inside strings without escaping them (e.g. `"C:\temp\file.txt"`), causing Python to parse `\t` as a tab character.

How to Fix

1Use absolute paths: Resolve paths dynamically relative to the script's file path using 'os.path.abspath' or 'pathlib.Path(__file__)'.
2Create missing parent directories: Use 'os.makedirs(exist_ok=True)' or 'pathlib.Path.mkdir(parents=True)' before writing files.
3Verify file existence: Check if the path exists using 'os.path.exists()' or 'pathlib.Path.exists()' before opening.

Python Operations & Verification

Locate files relative to the script file location using the pathlib module to prevent active terminal working directory confusion.

Pathlib Absolute Resolution Example
from pathlib import Path

# 1. Get parent folder of active script file
base_dir = Path(__file__).resolve().parent

# 2. Build target absolute path
config_path = base_dir / "config.json"

# 3. Read safely
if config_path.exists():
    with config_path.open("r") as f:
        print(f.read())
else:
    print(f"File missing at resolved path: {config_path}")

Platform Specific Fixes

Display the active terminal working directory (pwd) and check folder casing.

Linux Config
pwd
ls -la

Best Practices

  • Always rely on `pathlib` for file paths management, as it automatically normalizes slash directions across platforms.
  • Log resolved absolute path locations in debug outputs when handling files access tasks.

Frequently Asked Questions (FAQ)

Q: What is a FileNotFoundError?

It is a runtime exception raised when Python cannot find a file or directory at the specified path.

Q: Why does it fail when the file is right there?

This is almost always due to relative path confusion. Python resolves relative paths based on where you ran the python command, not where the script file is located. Run 'os.getcwd()' to inspect your active working directory.

Q: How do I build paths relative to my script file?

Use pathlib: 'from pathlib import Path; file_path = Path(__file__).parent / "data.txt"'. This ensures the path is resolved relative to the script's location, regardless of where it is executed from.

Q: Why does open('folder/file.txt', 'w') fail with FileNotFoundError?

While Python can create a new file, it cannot create new folders automatically. If 'folder/' does not exist, the call fails. Create the directory first using 'os.makedirs("folder", exist_ok=True)'.

Still having this problem?

Didn't solve your problem?

SuggestRequest Error