Files – Error Handling (try/except), online

Six activities your students do on their own devices. See who's finished, and their scores.

Files – Error Handling (try/except) in one minute

What students need to know

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.

Mistakes students make

  • Missing colon after try.
  • Error names are capitalized: FileNotFoundError.
  • try must come before except.
  • Python uses except, not catch.

Discover other journeys with this module

What students will learn in this module

Every question in Module 47

Quick questions

  1. What error occurs when reading a missing file?AFileNotFoundErrorBValueError
  2. Why use try/except with files?AFiles might not exist or be inaccessibleBIt makes code faster
  3. What does 'finally' do?AOnly runs on errorBAlways runs, even if there's an error
  4. Which is safer: try/except or with?ABoth together is safestBtry/except alone
  5. What error if you can't write to a file?AWriteErrorBPermissionError

Key terms

Students type the meaning of:

  • FileNotFoundError
  • PermissionError
  • finally:
  • except ___Error:

Match the pairs

open('missing.txt')Match it to what it does
try:
    f = open('f.txt')
except:
    pass
f.read()
Match it to what it does
try:
    f = open('f.txt')
except FileNotFoundError:
    print('Not found')
Match it to what it does
try:
    open('f.txt')
finally:
    print('done')
Match it to what it does
try:
    open('x.txt')
except FileNotFoundError:
    print('Missing')
Match it to what it does
try:
    open('x.txt')
except Exception as e:
    print(type(e).__name__)
Match it to what it does

Fill in the blanks

  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

Question 1

AtryB    with open('f.txt') as f:C        print(f.read())Dexcept FileNotFoundError:E    print('Missing')

Question 2

Atry:B    with open('f.txt') as f:C        print(f.read())Dexcept filenotfounderror:E    print('Missing')

Question 3

Aexcept:B    print('Error')Ctry:D    with open('f.txt') as f:E        print(f.read())

Question 4

Atry:B    with open('f.txt') as f:C        print(f.read())Dcatch FileNotFoundError:E    print('Missing')

Question 5

Atry:B    with open('f.txt') as f:C        print(f.read())Dexcept:E    pass  # silently ignore