ProjectStack
python

FileNotFoundError: [Errno 2] No such file or directory

Python tried to open or access a file that doesn't exist at the path you specified. Either the path is wrong, the file hasn't been created yet, or the working directory is different from what you expect.

Common causes

  • The file path is relative and your working directory isn't where you think it is
  • A typo in the filename or path, including wrong case on case-sensitive systems
  • The file was deleted, moved, or never created
  • You're using a relative path like 'data/file.csv' but running the script from a different directory

How to fix it

  1. Print the current working directory to verify your location: import os; print(os.getcwd())
  2. Use an absolute path or build one relative to the script: os.path.join(os.path.dirname(__file__), 'data.csv')
  3. Check that the file actually exists: ls <path> or os.path.exists(path)
  4. Run the script from the directory that contains the file, or adjust the path accordingly

Example

Traceback (most recent call last): File "app.py", line 3, in <module> with open('data/users.csv') as f: FileNotFoundError: [Errno 2] No such file or directory: 'data/users.csv'

Opening a CSV file with a relative path while running the script from the wrong directory

Have a different error?

Paste any error message into the Error Translator to get an instant explanation.