Exercises: File Input and Output
These exercises progress from foundational file operations to multi-step data processing pipelines. All exercises require writing Python code.
Difficulty Guide: - ⭐ Foundational (5-10 min each) - ⭐⭐ Intermediate (10-20 min each) - ⭐⭐⭐ Challenging (20-40 min each) - ⭐⭐⭐⭐ Advanced/Research (40+ min each)
Part A: Reading and Writing Text Files ⭐
A.1. Write a program that creates a text file called hello.txt containing the text "Hello, file system!" on the first line and "This is my first file." on the second line. Then read the file back and print its contents.
A.2. Write a function count_lines(filename: str) -> int that returns the number of lines in a text file. Test it on a file you create with at least 5 lines.
A.3. Write a program that reads a file called numbers.txt (one integer per line) and prints the sum. Create a test file with the values 10, 20, 30, 40, 50 (one per line) and verify the sum is 150.
A.4. Explain the difference between the following two code snippets. Which one is correct, and why?
# Snippet A
f = open("data.txt", "r")
content = f.read()
f.close()
# Snippet B
with open("data.txt", "r") as f:
content = f.read()
A.5. Write a function append_line(filename: str, line: str) -> None that appends a single line to a file (adding a newline at the end). Demonstrate that calling it three times adds three lines without erasing previous content.
A.6. What happens if you try to open a file in read mode ("r") that doesn't exist? What about write mode ("w")? What about append mode ("a")? Write a short program that demonstrates all three behaviors.
Part B: Line-by-Line Processing ⭐⭐
B.1. Write a function grep(filename: str, keyword: str) -> list[str] that returns all lines from a file containing the given keyword (case-insensitive). Test it on a file with at least 10 lines.
B.2. Write a program that reads a text file and creates a new file containing only the non-blank lines, with each line numbered. For example, if the input is:
Hello
World
Python
The output should be:
1: Hello
2: World
3: Python
B.3. Write a function word_count(filename: str) -> dict[str, int] that reads a text file and returns a dictionary mapping each word (lowercased) to its frequency. Ignore punctuation. Test it on a paragraph of text.
B.4. Dr. Patel has a file where each line contains a DNA sequence (just the letters A, T, C, G). Write a function nucleotide_stats(filename: str) -> dict[str, int] that counts the total occurrences of each nucleotide across all lines. Test with a file containing:
ATCGATCG
AATTCCGG
GGCCAATT
Expected output: {'A': 8, 'T': 8, 'C': 8, 'G': 8}
B.5. Write a program that reads a log file where each line has the format LEVEL: message (where LEVEL is INFO, WARNING, or ERROR). Count how many lines are at each level and print a summary. Create a test log file with at least 15 entries.
Part C: Paths and pathlib ⭐⭐
C.1. Using pathlib, write a program that:
1. Creates a directory called output (if it doesn't exist)
2. Creates a subdirectory called output/reports
3. Writes a file output/reports/summary.txt with some text
4. Prints the absolute path of the file
5. Prints the file's name, stem, suffix, and parent
C.2. Write a function find_files(directory: str, extension: str) -> list[Path] that uses pathlib to find all files in a directory (not subdirectories) with the given extension. For example, find_files(".", ".py") should return all Python files in the current directory.
C.3. Write a program that uses Path(__file__).parent to create a file called timestamp.txt in the same directory as the script, containing the current date and time. The program should work regardless of which directory the user runs it from.
Part D: CSV Processing ⭐⭐
D.1. Create a CSV file called students.csv with columns name, major, gpa. Add at least 6 students. Then write a program that reads the file and prints only students with a GPA above 3.5.
D.2. Write a function csv_to_dicts(filename: str) -> list[dict] that reads a CSV file using DictReader and returns a list of dictionaries (one per row). Write a companion function dicts_to_csv(data: list[dict], filename: str) -> None that writes a list of dictionaries to a CSV file using DictWriter. Test the round-trip: read a CSV, modify it, write it back.
D.3. Elena receives a CSV with columns date, donor_name, amount, campaign. Write a program that:
1. Reads the CSV
2. Computes the total donations per campaign
3. Writes a new CSV called campaign_totals.csv with columns campaign, total, donor_count
4. Prints which campaign raised the most
Create a test CSV with at least 10 rows and 3 different campaigns.
D.4. Write a program that merges two CSV files with the same columns into a single output CSV. The output should have one header row followed by all data rows from both files. Test with two small CSVs.
Part E: JSON Processing ⭐⭐⭐
E.1. Write a program that stores a dictionary of your favorite books (title → author) in a JSON file. The program should: - Load existing books from the file on startup (if the file exists) - Let the user add a new book - Save the updated collection back to the file
Test that the data persists across multiple runs of the program.
E.2. Write a save_config and load_config function pair that stores application settings as JSON. The config should be a dictionary with settings like {"theme": "dark", "font_size": 14, "autosave": true, "recent_files": ["a.txt", "b.txt"]}. If the config file doesn't exist, return default values.
E.3. Write a program that converts a CSV file to a JSON file. Each row in the CSV becomes one dictionary in a JSON array. Test with sample-data.csv from this chapter's code directory.
E.4. Write a program that converts a JSON array of objects (all with the same keys) to a CSV file. This is the reverse of E.3. Test by converting the JSON file from E.3 back to CSV and verifying it matches the original.
Part F: Integration and Challenge Problems ⭐⭐⭐⭐
F.1. Contact Manager. Build a simple contact manager that stores contacts as JSON. Each contact has name, phone, email, and category (friend/family/work). The program should support:
- Add a contact
- Search by name (partial, case-insensitive)
- List all contacts in a category
- Delete a contact by name
- Auto-save and auto-load
F.2. Grade Book with Export. Build a grade book program that: - Stores student records as JSON (name, student_id, list of assignment scores) - Computes each student's average and letter grade - Exports a summary to CSV (one row per student: name, student_id, average, letter_grade) - Can import new scores from a CSV file (student_id, assignment_name, score)
F.3. Log Analyzer. Write a program that analyzes a web server log file. Each line has the format:
2025-01-15 09:30:22 INFO GET /index.html 200
2025-01-15 09:30:23 ERROR GET /missing.html 404
Your program should: 1. Count requests by status code (200, 404, 500, etc.) 2. Find the most-requested pages 3. Count errors by hour of day 4. Write all results to both a text summary and a JSON file
Create a test log with at least 50 entries.
F.4. Data Pipeline. Elena needs to merge three monthly CSV files, each with the same columns. Write a program that:
1. Reads all .csv files from a directory using pathlib
2. Combines them into one dataset
3. Removes duplicate rows
4. Sorts by a specified column
5. Writes the result to both a combined CSV and a summary JSON
6. Handles the case where a CSV has different columns (skip it and print a warning)
Reflection Question
Think about a program you use daily (a notes app, a game, a social media client). What data does it need to persist? What format would you choose (text, CSV, JSON, or something else), and why? Write 3-4 sentences explaining your reasoning.