Module 7

FileNotFoundError

When a file does not exist.

A common exception

FileNotFoundError occurs when you try to open a file that does not exist. You can catch it with except.

Python
filename = "missing.txt"
try:
    with open(filename) as f:
        print(f.read())
except FileNotFoundError:
    print(f"File not found: {filename}")
File not found: missing.txt
Your task

Catch FileNotFoundError

  • Try to open a missing file and catch the error.