Project: CSV Contact Manager
Project: CSV Contact Manager
In this hands-on project, we will apply the advanced file handling techniques covered in this chapter—including csv.DictReader, csv.DictWriter, context managers, and robust error handling—to build a persistent Command-Line CSV Contact Manager.
1. Project Specifications & Requirements
The application will manage contact records and save them to contacts.csv. Key capabilities:
- 1Data Persistence: Automatically reads from and writes to a structured CSV file.
- 2Auto-Initialization: Gracefully initializes the CSV file with headers if it does not yet exist.
- 3Core CRUD Operations:
- Create: Add new contacts with validation.
- Read: Display all contacts in a formatted table.
- Search: Case-insensitive search by name or category.
- Delete: Remove a contact by name and rewrite the updated dataset.
- 1Safety: Handles missing files, invalid inputs, and file permissions without crashing.
2. Complete Application Code
3. Sample Execution Output
Multiple Choice Questions
1. In our Contact Manager, what happens if contacts.csv does not exist when the program boots?
A. The script crashes immediately with a FileNotFoundError B. initialize_storage() creates the file and writes the header row using csv.DictWriter C. Python waits for user input before attempting any file access D. An in-memory SQLite database is used instead Answer: B Explanation: initialize_storage() checks if the file exists; if not, it opens the file with mode 'w' and calls writer.writeheader() to establish the schema.
2. How does the delete_contact() function remove a record from the CSV file?
A. By seeking directly to the byte offset in the CSV file and issuing f.truncate() B. By loading records into memory, filtering out the deleted entry, and rewriting the entire file C. By writing an empty dictionary at the row index D. The csv module does not support deleting records Answer: B Explanation: In standard sequential text/CSV files, deleting a record requires filtering the in-memory list of records and rewriting the updated collection with save_contacts().
3. Why is newline='' supplied to open() during both load and save operations?
A. To convert single quotes to double quotes automatically B. To ensure consistent line termination across Windows, Linux, and macOS without extra blank lines C. To prevent Unicode characters from being parsed D. To disable buffering for faster disk writes Answer: B Explanation: The newline='' argument ensures Python's file object delegates all newline translation to the CSV reader/writer, preventing unwanted blank lines on Windows platforms.
4. Which method of csv.DictWriter writes all dictionary rows in a single step?
A. writer.dump_all() B. writer.write_batch() C. writer.writerows() D. writer.save() Answer: C Explanation: writer.writerows(iterable_of_dicts) accepts an iterable of row dictionaries and writes them all into the CSV file.
5. Why is csv.DictReader preferred over index-based csv.reader for storing entities with multiple attributes like contacts?
A. DictReader is written in C and runs 10x faster B. Attributes can be accessed safely by column name (e.g. c['phone']), making the code resilient to column reordering C. DictReader encrypts user passwords automatically D. DictReader handles binary image files Answer: B Explanation: DictReader accesses values by header names instead of fixed integer indices, preventing bugs when columns are added, removed, or reordered in the CSV file.
Creating Classes & Objects (Review)
Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
Related Lessons
| Previous Lesson | Next Lesson |
|---|---|
| File Exceptions and Error Handling | Creating Classes & Objects (Review) |
Practice Quiz
Test your understanding of this lesson with 5 questions. Each question has one correct answer.