High severity beginner · Fix: 2-5 min

FileNotFoundError

builtins.FileNotFoundError

What this error means
PyTorch raises FileNotFoundError when the specified dataset directory or file path does not exist or is incorrect.

Stack trace

traceback
Traceback (most recent call last):
  File "train.py", line 42, in <module>
    dataset = torchvision.datasets.ImageFolder(root='data/train', transform=transform)
  File "/usr/local/lib/python3.9/site-packages/torchvision/datasets/folder.py", line 273, in __init__
    raise FileNotFoundError(f"Dataset not found at {root}")
FileNotFoundError: Dataset not found at data/train
QUICK FIX
Confirm the dataset path exists on disk and update the path argument to the correct existing directory.

Why it happens

This error occurs because the path provided to the PyTorch dataset loader does not exist on the filesystem. It can happen if the dataset was not downloaded, the path is misspelled, or the working directory is incorrect.

Detection

Before dataset loading, check if the dataset path exists using os.path.exists() or os.path.isdir() to catch missing paths early and log a clear error message.

Causes & fixes

1

Dataset directory does not exist at the specified path

✓ Fix

Verify the dataset path is correct and the directory exists. Download or extract the dataset to that location before loading.

2

Relative path used but current working directory is different than expected

✓ Fix

Use absolute paths or ensure the script runs with the correct working directory where the dataset path is valid.

3

Typo or incorrect folder name in the dataset path string

✓ Fix

Double-check the folder names and spelling in the dataset path string passed to the dataset loader.

Code: broken vs fixed

Broken - triggers the error
python
import torchvision.datasets as datasets

# This will raise FileNotFoundError if 'data/train' does not exist
train_dataset = datasets.ImageFolder(root='data/train', transform=None)  # Error here
Fixed - works correctly
python
import os
import torchvision.datasets as datasets

# Use absolute path and check existence before loading
dataset_path = os.path.abspath('data/train')
if not os.path.isdir(dataset_path):
    raise FileNotFoundError(f"Dataset path not found: {dataset_path}")
train_dataset = datasets.ImageFolder(root=dataset_path, transform=None)  # Fixed
print('Dataset loaded successfully')
Added a check to confirm the dataset directory exists and converted the path to absolute to avoid relative path issues.

Workaround

Wrap dataset loading in try/except FileNotFoundError and prompt the user to download or specify the correct dataset path if missing.

Prevention

Use absolute dataset paths and verify dataset presence during setup or initialization steps to avoid runtime path errors.

Python 3.7+ · torchvision >=0.8.0 · tested on 0.15.x
Verified 2026-04
Verify ↗

Community Notes

No notes yetBe the first to share a version-specific fix or tip.