FileNotFoundError: No Such File or Directory

FileNotFoundError: [Errno 2] No such file or directory: 'data.csv'

Quick Answer

The file path does not exist. This is usually a relative path issue. Use absolute paths or pathlib.Path to build paths relative to the script location.

Why This Happens

Relative file paths are resolved from the current working directory, which may differ from the script's directory. Use __file__ to build paths relative to the script.

The Problem

with open('data/config.json') as f:
    config = json.load(f)

The Fix

from pathlib import Path
script_dir = Path(__file__).parent
config_path = script_dir / 'data' / 'config.json'
with open(config_path) as f:
    config = json.load(f)

Step-by-Step Fix

  1. 1

    Check the working directory

    Print os.getcwd() to verify.

  2. 2

    Use absolute paths

    Build paths with Path(__file__).parent / 'filename'.

  3. 3

    Verify the file exists

    Use Path(path).exists() to check before opening.

Bugsly catches this automatically

Bugsly's AI analyzes this error pattern in real-time, explains what went wrong in plain English, and suggests the exact fix — before your users even report it.

Try Bugsly free