Python

FileNotFoundError: [Errno 2] No such file or directory

FileNotFoundError

Python tried to open a file that doesn't exist at the given path.

Common Causes

Fixes

  1. 1. Use pathlib for cross-platform paths
    from pathlib import Path
    p = Path('dir') / 'file.txt'
  2. 2. Check before opening
    if Path(path).exists():
        with open(path) as f: ...
  3. 3. Use absolute path
    path = Path(__file__).parent / 'data.json'

Still not fixed?

Related Errors

DuskTools That Might Help

All Errors