← Python

Files – Error Handling (try/except)

A try/except block around file operations catches FileNotFoundError and other IO errors

What it does: Catching FileNotFoundError specifically handles the most common file error. The finally block runs whether or not an exception occurred — great for cleanup. FileNotFoundError is specific to missing files — ValueError is for bad values. Files are outside your program's control — they can be missing, locked, or unreadable. with handles closing; try/except handles the error — use both together. PermissionError means the OS denied access — WriteError doesn't exist in Python. Stack multiple except blocks, or catch a tuple: except (TypeError, ValueError):. The except block only runs when the specific error is raised.

Common mistakes: Missing colon after try. Error names are capitalized: FileNotFoundError.

Class: _____________________  ·  Name: _____________________  ·  Date: _________

Free printable worksheet with full answer key — or add this module to a journey and assign it to your class.

Take this worksheet further — free with Cloodit

Each concept is reinforced from every angle — learned, then applied, then tested.

Learning
Duo
What error occurs when reading a missing file?
FileNotFoundErrorValueError
Matches
❌ open('missing.txt')
Error: FileNotFoundError
Applying
Meanings
Term
FileNotFoundError
Meaning
Error when trying to read a file that doesn't exist.
Practice
try:
Testing
Gaps
except :
Check Answer
Spots
1try
2 with open('f.txt') as f:

Cloodit turns curriculum-aligned coding content into journeys students work through one biome at a time.

The 'Cosmic Owl' badge — Comprehension Guru, one of Cloodit's 106 collectible badges
Students earn collectable badges

Over 100 of them, across seven natural biomes.

Study Buddy
Students build a real study resource

Turns their work into revision material ready for Flash Cards and Exam study.

Diamond Dash — one of Cloodit's live, competitive Sprint games
Students lock in with Sprints

Live, competitive practice for the start of the lesson or a teacher-led lesson finisher.

A Cloodit certificate of achievement, ready to personalise and print
Custom printable certificates

Honour your top performers, or encourage a student who's trying hard — ready to print in seconds.

Quick Questions

1. What error occurs when reading a missing file?
A. FileNotFoundError ✓
B. ValueError
FileNotFoundError is specific to missing files — ValueError is for bad values
2. Why use try/except with files?
A. It makes code faster
B. Files might not exist or be inaccessible ✓
Files are outside your program's control — they can be missing, locked, or unreadable
3. What does 'finally' do?
A. Always runs, even if there's an error ✓
B. Only runs on error
finally runs whether the try succeeded or failed — good for cleanup
4. Which is safer: try/except or with?
A. try/except alone
B. Both together is safest ✓
with handles closing; try/except handles the error — use both together
5. What error if you can't write to a file?
A. PermissionError ✓
B. WriteError
PermissionError means the OS denied access — WriteError doesn't exist in Python

Match the Pairs

open('missing.txt') Error: FileNotFoundError
try: f = open('f.txt') except: pass f.read() Error: f may not be defined
try: f = open('f.txt') except FileNotFoundError: print('Not found') Valid: Handle missing file
try: open('f.txt') finally: print('done') Ok: finally always runs
try: open('x.txt') except FileNotFoundError: print('Missing') Output: Missing
try: open('x.txt') except Exception as e: print(type(e).__name__) Output: FileNotFoundError

Key Term

FileNotFoundError
Error when trying to read a file that doesn't exist.~~PermissionError

Fill in the Blanks

1. try: with open('f.txt') as f: print(f.read()) except FileNotFoundError: print('Not found')
2. try: with open('f.txt') as f: print(f.read()) except Exception as e: print(e)
3. try: f = open('f.txt') except FileNotFoundError: print('Missing')
4. try: with open('f.txt') as f: print(f.read()) except PermissionError: print('No access')
5. try: open('f.txt') except FileNotFoundError: print('Missing') finally: print('Done')

Spot the Error

Question 1
A try
B with open('f.txt') as f:
C print(f.read())
D except FileNotFoundError:
E print('Missing')
Line A — Missing colon after try
Question 2
A try:
B with open('f.txt') as f:
C print(f.read())
D except filenotfounderror:
E print('Missing')
Line D — Error names are capitalized: FileNotFoundError
Question 3
A except:
B print('Error')
C try:
D with open('f.txt') as f:
E print(f.read())
Line A — try must come before except
Question 4
A try:
B with open('f.txt') as f:
C print(f.read())
D catch FileNotFoundError:
E print('Missing')
Line D — Python uses except, not catch
Question 5
A try:
B with open('f.txt') as f:
C print(f.read())
D except:
E pass # silently ignore
Line E — Bare except with pass silently hides ALL errors. Catch specific errors.
For Teachers

Create a free class, add this module to a journey, and track who's stuck.

Create a free teacher account →

No credit card. No setup fee. Make a class and invite students in under three minutes.

For Students

Practice this topic interactively, track progress, and unlock badges.

Start Learning Free →

Have a class code?