ShareYourGroupShareYourGroupPython

FileNotFoundError: no such file or directory

A FileNotFoundError means Python tried to open a file for reading and found nothing at the place you named. The file may exist somewhere, but not where the program looked.

with open("report.txt", encoding="utf-8") as f:
    print(f.read())

What Python says

Traceback (most recent call last):
  File "example.py", line 1, in <module>
    with open("report.txt", encoding="utf-8") as f:
         ~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
FileNotFoundError: [Errno 2] No such file or directory: 'report.txt'

The message quotes the name exactly as Python received it. Look at it closely, because the fault is often visible right there.

Cause 1: the program is looking in a different folder

A bare name such as "report.txt" means "in the current working folder". That is the folder the program was started from, which is not always the folder where the program file is saved. To see where Python is looking:

import os
print(os.getcwd())
print(os.listdir())

The first line prints the current folder and the second lists the files in it. If your file is not in that list, either move the file there or give the path to where it is.

Cause 2: the name is not exactly right

Cause 3: backslashes in a Windows path

Windows paths use backslashes, and the backslash is also Python's escape character inside strings. In "C:\new\table.txt", Python reads \n as a new line and \t as a tab, and the path is ruined. Three safe ways to write it:

print(r"C:\new\table.txt")
print("C:/new/table.txt")
print("C:\\new\\table.txt")

Output

C:\new\table.txt
C:/new/table.txt
C:\new\table.txt

The r before the string makes it a raw string, in which backslashes are left alone. Forward slashes also work in Python on Windows, and are the simplest choice.

Cause 4: the folder does not exist

Writing with mode "w" creates the file, but not the folders above it.

with open("reports/2026/summary.txt", "w", encoding="utf-8") as f:
    f.write("hello")

What Python says

Traceback (most recent call last):
  File "example.py", line 1, in <module>
    with open("reports/2026/summary.txt", "w", encoding="utf-8") as f:
         ~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
FileNotFoundError: [Errno 2] No such file or directory: 'reports/2026/summary.txt'

Create the folders first with os.makedirs("reports/2026", exist_ok=True).

Checking before opening

import os
print(os.path.exists("report.txt"))

Output

False

os.path.exists returns True or False. It is useful for a message such as "no saved game found".

Handling it in the program

When a missing file is a normal situation, such as the first run of a program before any data has been saved, catch the error and carry on with a default.

try:
    with open("scores.txt", encoding="utf-8") as f:
        best = int(f.read())
except FileNotFoundError:
    best = 0
print("Best score so far:", best)

Output

Best score so far: 0

Building paths that work everywhere

To open a file that sits beside your program no matter where the program is started from, build the path from the program's own location with the pathlib module.

from pathlib import Path

folder = Path(__file__).parent
with open(folder / "report.txt", encoding="utf-8") as f:
    print(f.read())

__file__ is the path of the running program. .parent is its folder. The slash joins a folder and a file name correctly on every system.

PermissionError means the file exists but you are not allowed to open it, or it is a folder, or it is open in another program such as a spreadsheet. IsADirectoryError means the path points to a folder. Reading and writing files is covered in lesson 18.

Other error messages

Examples on this page were run with Python 3.14 on 12 November 2026. The output shown is the real output.

This is an independent learning site. It is not connected with or approved by the Python Software Foundation. "Python" is a trademark of the Python Software Foundation and is used here only to name the language.