Script Valley
Python: Complete Language Course
File I/O and Error Handling/Assessment

Practice & Assessment

Test your understanding of File I/O and Error Handling

Multiple Choice Questions

5
1

Why should you always use the `with` statement when opening files?

2

What is the difference between `json.dumps` and `json.dump`?

3

What is wrong with `except:` (bare except) in Python?

4

Why should CSV files be opened with `newline=""`?

5

What does `raise RuntimeError('msg') from e` do differently than `raise RuntimeError('msg')`?

Coding Challenges

1
1

JSON config file reader with validation

Write a function `load_config(filepath)` that reads a JSON file, validates it contains required keys ('host', 'port', 'debug'), raises a custom `ConfigError` if the file is missing or malformed or if required keys are absent. Return the config dict if valid. Test with a valid file, a missing file, invalid JSON, and a file missing required keys. Input: file path string. Output: dict or raised custom exception. Estimated time: 20-25 minutes.

Medium

Mini Project

1

CSV Contact Book with JSON Backup

Build a CLI contact book. Contacts have name, email, and phone. Store data in a CSV file (contacts.csv) using DictReader/DictWriter. Support operations: add contact, search by name (partial match), delete by name, list all contacts, and export all contacts to a JSON backup file (contacts.json). Wrap all file operations in try/except blocks with descriptive custom exceptions (ContactNotFoundError, DuplicateContactError). All changes persist to disk after each operation. Demonstrate all module skills: file I/O, JSON, CSV, custom exceptions, and the with statement.

Medium