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
Check the working directory
Print os.getcwd() to verify.
- 2
Use absolute paths
Build paths with Path(__file__).parent / 'filename'.
- 3
Verify the file exists
Use Path(path).exists() to check before opening.
Got the actual stack trace?
Paste it into our free AI explainer to get the cause and the fix for your specific case — no signup, nothing to install.
Explain my errorBugsly 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