Unit 11: File I/O and Exceptions
Reading and writing outside the program's own memory, and catching the errors that show up when the outside world misbehaves.
6 topics · 20 questions
Each topic is a path, not a menu: work down it in order and every question builds on the one above. Click any of them to watch the answer draw itself.
Opening, Reading, and Closing a File
- How it worksHow does opening a file hand the program a stream it can read one line or one chunk at a time?
- Worked exampleTrace opening notes.txt, calling readline() twice, then closing the file.
- Common mistakeWhy doesn't forgetting to close a file always cause an immediate, visible error?
- Going furtherWhat happens to a program's open file handles once a script opens a hundred files without closing any?
Your turn1 of 2
A script opens 140 files during its run and forgets to close any of them. How many open file handles does the script accumulate?Writing to a File
- How it worksHow does append mode add new lines to the end of a file instead of overwriting what's already there?
- Worked exampleTrace opening log.txt in write mode, writing hello then world, and reading the file back.
- Going furtherWhat changes about a file's contents once write mode is swapped for append mode on the same code?
Your turn1 of 2
A file is opened in write mode and 70 lines are written to it, each 130 characters long. How many total characters does the file now contain?Using a Context Manager to Handle a File
- How it worksHow does a with block automatically call close on a file the moment its indented block ends?
- Worked exampleTrace with open('data.txt') as f: lines = f.readlines() and confirm f is closed once the block ends.
- Common mistakeWhy isn't manually calling close always as safe as using a with block?
Your turn1 of 2
A script runs 70 separate with blocks in sequence, one after another, each opening and then automatically closing one file. How many total files does the script open across all n blocks?try / except and Catching an Error
- How it worksHow does a try block hand control to its except block the instant an error is raised inside it?
- Worked exampleTrace try: 10 / 0 except ZeroDivisionError: print('cannot divide by zero').
- Common mistakeWhy doesn't wrapping code in try except silently fix whatever bug caused the error?
- Going furtherWhat happens once a try block is followed by several except blocks for different error types?
Your turn1 of 2
A try block runs 22 risky operations in sequence, and the 2-th one raises an error that stops the try block immediately. How many of the n operations actually ran before the error was raised, counting the one that raised it?Raising and Propagating Exceptions
- How it worksHow does an uncaught exception unwind the call stack, popping one frame at a time, until something catches it?
- Worked exampleTrace an exception raised inside inner(), called by middle(), called by outer(), with no except block anywhere.
- Going furtherWhat happens once middle() wraps its call to inner() in its own try except?
Your turn1 of 2
A chain of 15 nested function calls, each calling the next, has an exception raised inside the innermost one, with no except block anywhere in the chain. How many total frames does the unwinding pop before the program crashes?Custom Exceptions and Defensive Programming
- How it worksHow does defining a new exception class let a program raise and catch its own specific kind of error?
- Worked exampleTrace class InsufficientFundsError(Exception): pass, raised inside withdraw() when amount exceeds balance.
- Ties togetherHow does checking a precondition and raising an exception connect back to a function's guard clauses?
Your turn1 of 2
A withdraw() function checks its precondition and raises a custom InsufficientFundsError whenever the requested amount exceeds the balance. Out of 142 withdrawal attempts, 6 request more than the available balance. How many of the n attempts raise the custom error?