How to Fix: FileNotFoundError: [Errno 2] No such file or directory

3D illustration of a searchlight finding an empty spot in a file cabinet, representing the FileNotFoundError in Python.

You tried to open a file in Python, but it crashed with this error, resulting in a FileNotFoundError Python message. However, understanding the common causes can help prevent this Python error from occurring.

It simply means: Python looked where you told it to, but nothing was there.

โšก Quick Fix: FileNotFoundError: [Errno 2] No such file or directory โ€” Python Relative Path, Typo, and os.path Fix

Python looked exactly where you told it to look โ€” and found nothing. Either the filename has a typo, the extension is hidden, or your script runs from a different folder than the file lives in.

# WRONG โ€” relative path breaks when you run from a different directory
with open("data.txt", "r") as f:    # crashes if CWD != script folder
    print(f.read())

# WRONG โ€” case matters on Mac/Linux
with open("Data.txt", "r") as f:    # crashes if file is named data.txt
    print(f.read())

# RIGHT โ€” build an absolute path anchored to the script's own location
import os

script_dir = os.path.dirname(os.path.abspath(__file__))
file_path = os.path.join(script_dir, "data.txt")

with open(file_path, "r") as f:
    print(f.read())

The two causes below explain exactly why Python looks in the wrong folder and how to verify your working directory in three seconds.

Cause 1: A Simple Typo (Most Common)

Did you type data.txt when the file is actually named Data.txt? Such small discrepancies can lead to a FileNotFoundError in Python.

  • Windows is often case-insensitive, but Mac/Linux are NOT.
  • Always check the exact spelling and capitalisation.
  • Check the extension! sometimes a file is named data.txt.txt if you have extensions hidden in Windows Explorer.

Cause 2: The “Relative Path” Trap (VS Code Users Beware!)

This is the #1 tricky cause. If your code is open("data.txt", "r"), Python only looks in the Current Working Directory (where you ran the command from). This is a common mistake that can trigger a FileNotFoundError Python when the expected file isn’t found in the location.

If your project looks like this:

/MyProject
    /code
        script.py
    data.txt

If you run the script from the /MyProject folder, it might fail because data.txt is NOT next to script.py.

The Fix: Use Absolute Paths so Python always knows exactly where to look, no matter where you run the script from.

import os

# Get the folder where THIS script.py file lives
script_dir = os.path.dirname(os.path.abspath(__file__))

# Build the full path to the data file securely
file_path = os.path.join(script_dir, "data.txt")

with open(file_path, "r") as f:
    print(f.read())

Summary

If you are sure the file exists, 99% of the time the issue is that Python is looking in the wrong folder. Use os.path.abspath to fix it and avoid a FileNotFoundError Python.


FileNotFoundError: [Errno 2] No such file or directory โ€” Two Checks, One Permanent Fix

FileNotFoundError: [Errno 2] No such file or directory has two causes and one permanent fix.

Check the filename first. Python is case-sensitive on Mac and Linux โ€” data.txt, Data.txt, and DATA.TXT are three different files. Check the extension too. Windows hides extensions by default, so a file you named data.txt might actually be data.txt.txt on disk. Enable “Show file name extensions” in Windows Explorer and look again.

Check your working directory second. Run print(os.getcwd()) at the top of your script. Python prints the exact folder it looks in when you use a relative path like “data.txt”. If that folder isn’t where your file lives, your path is wrong โ€” not your filename.

Apply the permanent fix: anchor every file path to the script’s own location.

import os
script_dir = os.path.dirname(os.path.abspath(file))
file_path = os.path.join(script_dir, “data.txt”)

Add these three lines to every script that reads a file. The path works correctly whether you run the script from the terminal, from VS Code, from PyCharm, or from a cron job โ€” the working directory stops mattering entirely.

Similar Posts

Leave a Reply