ultralytics/yolov5 · error · FileNotFoundError

{p} does not exist

Error message

{p} does not exist

What it means

LoadImagesAndVideos (utils/dataloaders.py) raises FileNotFoundError when an entry of the source list/path is neither a glob pattern (contains '*'), nor a directory, nor a file after resolve(). This dataset class is used by detect.py/val.py for inference-time media loading; each item must be a literal existing path or a wildcard pattern.

Source

Thrown at utils/dataloaders.py:282

class LoadImages:
    """YOLOv5 image/video dataloader, i.e. `python detect.py --source image.jpg/vid.mp4`."""

    def __init__(self, path, img_size=640, stride=32, auto=True, transforms=None, vid_stride=1):
        """Initializes YOLOv5 loader for images/videos, supporting glob patterns, directories, and lists of paths."""
        if isinstance(path, str) and Path(path).suffix == ".txt":  # *.txt file with img/vid/dir on each line
            path = Path(path).read_text().strip().splitlines()
        files = []
        for p in sorted(path) if isinstance(path, (list, tuple)) else [path]:
            p = str(Path(p).resolve())
            if "*" in p:
                files.extend(sorted(glob.glob(p, recursive=True)))  # glob
            elif os.path.isdir(p):
                files.extend(sorted(glob.glob(os.path.join(p, "*.*"))))  # dir
            elif os.path.isfile(p):
                files.append(p)  # files
            else:
                raise FileNotFoundError(f"{p} does not exist")

        images = [x for x in files if x.split(".")[-1].lower() in IMG_FORMATS]
        videos = [x for x in files if x.split(".")[-1].lower() in VID_FORMATS]
        ni, nv = len(images), len(videos)

        self.img_size = img_size
        self.stride = stride
        self.files = images + videos
        self.nf = ni + nv  # number of files
        self.video_flag = [False] * ni + [True] * nv
        self.mode = "image"
        self.auto = auto
        self.transforms = transforms  # optional
        self.vid_stride = vid_stride  # video frame-rate stride
        if any(videos):
            self._new_video(videos[0])  # new video
        else:
            self.cap = None

View on GitHub (pinned to 20d1d78a08)

Solutions

  1. Print the resolved path from the same cwd to confirm: python -c "from pathlib import Path; print(Path('src').resolve(), Path('src').exists())".
  2. Use absolute paths in source lists and .txt manifests.
  3. Use a wildcard ('dir/*.jpg') so the glob branch handles missing matches instead of failing on existence.
  4. Prune dead lines from txt manifests before running.

Example fix

# before
dataset = LoadImagesAndVideos('clips/day1.mp4')  # typo'd name

# after
dataset = LoadImagesAndVideos('/data/clips/day1.mp4')
Defensive patterns

Strategy: validation

Validate before calling

from pathlib import Path
import glob as _glob

def sources_readable(path) -> bool:
    if isinstance(path, str) and Path(path).suffix == '.txt':
        path = Path(path).read_text().strip().splitlines()
    items = sorted(path) if isinstance(path, (list, tuple)) else [path]
    for p in items:
        p = str(Path(p).resolve())
        if '*' in p:
            continue
        if not (Path(p).is_dir() or Path(p).is_file()):
            return False
    return True

Try / catch

try:
    dataset = LoadImagesAndVideos(source)
except FileNotFoundError as e:
    raise SystemExit(f'missing inference source: {e}') from e

Prevention

When it happens

Trigger: run(source='vids/demo.mp4') with a typo or wrong cwd; a .txt list containing one dead entry (every line is resolved individually); paths that contain no '*' but reference a missing mount; passing a URL without a recognized scheme so it is treated as a path.

Common situations: Relative paths resolved from a different working directory; txt manifests generated on another machine with absolute paths; NFS mounts not yet attached at job start.

Related errors


AI-assisted analysis of ultralytics/yolov5@20d1d78a08 (2026-08-15). Data as JSON: /api/errors/a415e4395e7dfb15. Report an issue: GitHub.