← Back to the Introduction to Computer Science outline The Course Maker
← Introduction to Computer Science outline
Week 12 · Module overview

Week 12 — File Input/Output & Exceptions

Introduction to Computer Science Free sample edition

Welcome to your Week 12 Start Here page.

Why this matters

A field-biology student saving each day's plant counts for the next run and a small-business owner reloading last week's orders both need a program that remembers between runs and keeps going when one line of input is wrong.

In plain language

Every program you have written so far forgets everything the moment it stops. This week you give a program a memory: a file (a named store of text on your computer's disk that keeps its contents after the program ends), which one run can write and the next run can read back. You also learn what to do when something goes wrong while a program runs. Python reports each problem as an exception (a signal that names the kind of problem, such as a missing file or text that is not a number), and you can catch the ones you expect so the program keeps going. As always, you predict what each program prints and then run it to check.

  • Already covered: strings and strip, lists, dictionaries, and reading an error's last line.
  • This week: writing and reading files, converting what you read, and catching errors by type.
  • How they connect: every line you read is a string, so strip and int turn it into a number.
  • Where we're going next: Week 13, Algorithms: Searching, Sorting & Growth.

Focus

How does a program remember its data between runs, and keep running when one line of input is wrong?

By the end of this module, you should be able to:

  • [ ] write lines to a file and read them back as numbers
  • [ ] name a runtime error by its type
  • [ ] predict which block of a try statement runs

To-do

Plan your week

  1. Read Chapter 12 (about 45 minutes), then the lecture and slides.
  2. Work the lecture tutorial (60–90 minutes) and the practice set (about 20 minutes).
  3. Do the Coding Lab (about 90 minutes), the discussion, and the assignment.
  4. Take the quiz.

This week's checkpoint: predict what a program reads back from a file it wrote, then run it to check.

You've got this

Predict, then run, and ask your instructor if a step snags.

Built with The Course Maker · samples.thecoursemaker.com