TryBugHunt

FileNotFoundError: [Errno 2] No such file or directory

FileNotFoundError: [Errno 2] No such file or directory in Python means python looked for the file at the path you gave and nothing was there. Most often the path is relative and Python is looking in a different folder from the one your script is in.

PythonPattern: Logic errors

How do I fix FileNotFoundError: [Errno 2] No such file or directory?

FileNotFoundError: [Errno 2] No such file or directory means no file exists at the path you passed to open(). Most often the path is relative, and Python looks in the folder your terminal started in, not your script's folder. Build the path from the script instead: Path(__file__).parent / "data.txt".

  1. 1. Read the path shownThe end of the message shows the exact path Python tried. Compare it with where the file really is.
  2. 2. Print the working folderprint(os.getcwd()) and print(os.listdir()) show where Python is looking and the real file names.
  3. 3. Create missing foldersWriting to output/report.txt fails if output does not exist. Call os.makedirs("output", exist_ok=True) first.

What does FileNotFoundError: [Errno 2] No such file or directory mean?

A path like "data.txt" is relative: Python looks for it in the current working directory, which is the folder your terminal or editor was in when the program started. That is not always the folder your .py file is in. The message ends with the path Python actually tried, which is the best clue you have.

Why does FileNotFoundError: [Errno 2] No such file or directory happen?

1. A relative path read from the wrong folder

Running the script from another folder changes where "data.txt" points. Build the path from the script's own location instead.

Breaks

with open("data.txt") as f:
    print(f.read())

Works

from pathlib import Path

here = Path(__file__).parent
with open(here / "data.txt") as f:
    print(f.read())

2. A typo or a hidden file extension

Windows hides extensions, so a file shown as data.txt may really be data.txt.txt. Case also matters on Linux and Mac.

Breaks

open("Data.txt")   # file is data.txt

Works

import os
print(os.listdir("."))   # see the real names
open("data.txt")

3. Writing into a folder that does not exist

Opening a file for writing creates the file, but not the folders above it.

Breaks

open("output/report.txt", "w")

Works

import os
os.makedirs("output", exist_ok=True)
open("output/report.txt", "w")

How to find it in your own code

Checklist: 1) Read the path at the end of the message. 2) print(os.getcwd()) to see which folder Python is looking in. 3) print(os.listdir()) to see the files that are really there, with their exact names. 4) Use Path(__file__).parent / "name" for files that live next to your script. 5) Code that runs in a browser cannot read files from your computer's disk; paste the data into the code instead.

Practise logic errors

None of the challenges raises this exact error yet. These practise the same kind of bug.

Still not sure why yours breaks?

Paste it into the visualizer and watch it run line by line, with every variable at every step. Free, and it runs in your browser.

Related error guides

View all logic errors →