FileNotFoundError: [Errno 2] No such file or directory: 'data.csv'
Python can’t find the file at the path you specified.
Fix 1: Check the path
# ❌ File doesn't exist at this path
data = open("data.csv")
# ✅ Check if it exists first
import os
print(os.path.exists("data.csv")) # False — wrong path!
print(os.getcwd()) # Check where Python is running from
Fix 2: Use absolute paths
# ❌ Relative path depends on where you run the script from
open("data/file.csv")
# ✅ Path relative to the script file
from pathlib import Path
script_dir = Path(__file__).parent
file_path = script_dir / "data" / "file.csv"
data = open(file_path)
Fix 3: Wrong working directory
If you run python src/main.py from the project root, the working directory is the project root, not src/. Relative paths resolve from where you run the command, not where the script lives.
# Working directory is /project
cd /project
python src/main.py
# open("data.csv") looks for /project/data.csv, not /project/src/data.csv
Fix 4: Create the directory first
from pathlib import Path
# ❌ Directory doesn't exist
with open("output/results.csv", "w") as f:
f.write("data")
# ✅ Create directory if needed
Path("output").mkdir(parents=True, exist_ok=True)
with open("output/results.csv", "w") as f:
f.write("data")
Fix 5: Check for typos and case sensitivity
Linux/macOS file systems are case-sensitive:
# ❌ Wrong case on Linux
open("Data.CSV") # File is actually "data.csv"
# ✅ Match exact case
open("data.csv")
See also: Python cheat sheet | Python ModuleNotFoundError fix