How to fix FileNotFoundError in Jupyter Notebook & Google Colab
You’re sure the CSV or image is in the folder. But when you run the cell, Jupyter or Colab says:
FileNotFoundError: [Errno 2] No such file or directory: 'data/train.csv'
This is one of the most confusing early errors in data science – mostly because it’s not about Python at all, it’s about **where your notebook is looking from**.
Step 1: Understand “working directory” vs “file location”
Two things matter:
- File location: where your file actually exists on disk.
- Working directory: the folder Python is treating as “current” when you run
open()orpd.read_csv().
If those two don’t match, you get FileNotFoundError – even if you can see the file in your file
explorer.
data/ folder isn’t under that folder, it can’t find it.
Step 2: Fix FileNotFoundError in local Jupyter Notebook
1. Print your current working directory
In a new cell, run:
import os print(os.getcwd())
This shows the folder Jupyter is currently running in. Compare it to where your file actually lives.
2. Use project-relative paths, not absolute desktop paths
Instead of this:
df = pd.read_csv("C:/Users/me/Desktop/data/train.csv")
Prefer a project structure like:
project/
notebook.ipynb
data/
train.csv
test.csv
And use:
df = pd.read_csv("./data/train.csv")
3. Move the notebook or change the working directory
If your notebook is inside a notebooks/ folder, but your data is up one level, you have two options:
- Move the notebook to the project root, or
- Change working directory at the top of the notebook:
import os
os.chdir("..") # go up one folder
Step 3: Fix FileNotFoundError in Google Colab
1. Confirm where your files live in Drive
Typical flow:
- Mount Drive:
from google.colab import drive
drive.mount('/content/drive')
- Inspect the path in the left-hand file browser (e.g.
/content/drive/MyDrive/colab-data/train.csv). - Use that path (or a relative one) in your code.
2. Avoid hard-coded “Downloads” or random paths
Dragging a file into Colab’s temporary /content file space works until the runtime resets.
Prefer a stable folder under Drive, like:
/content/drive/MyDrive/projects/my-ml-project/
notebook.ipynb
data/
train.csv
3. Use os.listdir() to debug
If you’re not sure what’s in a folder, list it from the notebook:
import os
print(os.listdir())
print(os.listdir("./data"))
If train.csv doesn’t appear there, Python is correct: the file isn’t where the path says it is.
Step 4: Make your data paths stable and shareable
Fixing the error for yourself is step one. The next step is making sure your **future self and collaborators** don’t hit the same FileNotFoundError.
1. Standardise your project layout
Adopt a simple pattern for all your ML projects:
project-name/
notebook.ipynb
data/
raw/
processed/
models/
capsules/ # NoteCapsule snapshots
2. Use NoteCapsule to capture data layout
NoteCapsule can create a Capsule that includes a data_manifest.json file describing which data
paths your notebook expects.
!pip install notebookcapsule -q
from notebookcapsule import create_capsule
create_capsule(
name="after-fixing-filenotfounderror",
data_dirs=["./data"]
)
That way, anyone opening your project later can see exactly which files need to exist and where.
Quick checklist for fixing FileNotFoundError
- ✅ Print
os.getcwd()to see where Python is “standing”. - ✅ Use project-relative paths like
./data/train.csv. - ✅ Keep notebooks and data in a consistent folder structure.
- ✅ In Colab, mount Drive and use stable paths under
/content/drive/MyDrive/.... - ✅ Use NoteCapsule Capsules to record data layout and avoid mystery paths in the future.
Want fewer “why can’t it find the file?” bugs?
NoteCapsule helps you capture your notebook, dependencies, and data layout together – so sharing or revisiting your project doesn’t trigger a wave of FileNotFoundError.
Get early access to NoteCapsule
Join from the homepage and we’ll send you a quick setup guide and an example Capsule with a clear
data_manifest.json.