Files – Error Handling (try/except)

Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.

Files – Error Handling (try/except)

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. What error occurs when reading a missing file?
A FileNotFoundError
B ValueError
2. Why use try/except with files?
A Files might not exist or be inaccessible
B It makes code faster
3. What does 'finally' do?
A Only runs on error
B Always runs, even if there's an error
4. Which is safer: try/except or with?
A Both together is safest
B try/except alone
5. What error if you can't write to a file?
A WriteError
B PermissionError

Match the Pairs

Draw a line connecting each item on the left to its match on the right.

Left columnRight column
1open('missing.txt') AOutput: Missing
2try: f = open('f.txt') except: pass f.read() BValid: Handle missing file
3try: f = open('f.txt') except FileNotFoundError: print('Not found') COutput: FileNotFoundError
4try: open('f.txt') finally: print('done') DError: f may not be defined
5try: open('x.txt') except FileNotFoundError: print('Missing') EError: FileNotFoundError
6try: open('x.txt') except Exception as e: print(type(e).__name__) FOk: finally always runs

Definitions

FileNotFoundError

Fill in the Blanks

Complete each line of code by filling in the missing parts.

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

Spot the Error

Shade in the letter of the line that contains an error.

Question 1
A try
B with open('f.txt') as f:
C print(f.read())
D except FileNotFoundError:
E print('Missing')
Question 2
A try:
B with open('f.txt') as f:
C print(f.read())
D except filenotfounderror:
E print('Missing')
Question 3
A except:
B print('Error')
C try:
D with open('f.txt') as f:
E print(f.read())
Question 4
A try:
B with open('f.txt') as f:
C print(f.read())
D catch FileNotFoundError:
E print('Missing')
Question 5
A try:
B with open('f.txt') as f:
C print(f.read())
D except:
E pass # silently ignore

Answer Key

Files – Error Handling (try/except)
Section A: Quick Questions
Q1
A — FileNotFoundError
Q2
A — Files might not exist or be inaccessible
Q3
B — Always runs, even if there's an error
Q4
A — Both together is safest
Q5
B — PermissionError
Section B: Match the Pairs
Q1
open('missing.txt') → Error: FileNotFoundError
Q2
try: f = open('f.txt') except: pass f.read() → Error: f may not be defined
Q3
try: f = open('f.txt') except FileNotFoundError: print('Not found') → Valid: Handle missing file
Q4
try: open('f.txt') finally: print('done') → Ok: finally always runs
Q5
try: open('x.txt') except FileNotFoundError: print('Missing') → Output: Missing
Q6
try: open('x.txt') except Exception as e: print(type(e).__name__) → Output: FileNotFoundError
Section C: Definitions
Q1
FileNotFoundError — Error when trying to read a file that doesn't exist.~~PermissionError
Section D: Fill in the Blanks
Q1
try: with open('f.txt') as f: print(f.read()) except FileNotFoundError: print('Not found')
Q2
try: with open('f.txt') as f: print(f.read()) except Exception as e: print(e)
Q3
try: f = open('f.txt') except FileNotFoundError: print('Missing')
Q4
try: with open('f.txt') as f: print(f.read()) except PermissionError: print('No access')
Q5
try: open('f.txt') except FileNotFoundError: print('Missing') finally: print('Done')
Section E: Spot the Error
Q1
A — Missing colon after try
Q2
D — Error names are capitalized: FileNotFoundError
Q3
A — try must come before except
Q4
D — Python uses except, not catch
Q5
E — Bare except with pass silently hides ALL errors. Catch specific errors.