🐍 Python for Everyone

← Back to interview questions

Chapter 13 Project 50 min build

Log-File Analyzer

A menu-driven log-file analyzer built around file handling and the csv module as its core organizing principle — every operation reads a real log file with with open(...), processes it using plain Python (dicts, loops, string methods) from earlier chapters, and the export option writes a real CSV report using csv.writer. A try/except FileNotFoundError guards the very first read so a missing log file fails gracefully with a helpful message instead of crashing, exactly like Chapter 12's exception-handling pattern. Each operation lives in its own small function, matching Chapter 10's function-library pattern.

Example run

Output
=== Log-File Analyzer ===
Loaded 30 line(s) from 'server.log'.

1. Show counts by log level
2. Search for a keyword
3. Show the most common error message
4. Export summary to CSV
5. Quit
Choose an option (1-5): 1

Counts by level:
  INFO     14
  WARNING  5
  ERROR    11

1. Show counts by log level
2. Search for a keyword
3. Show the most common error message
4. Export summary to CSV
5. Quit
Choose an option (1-5): 3

Most common error: 'Database connection timeout' (5 occurrence(s))

1. Show counts by log level
2. Search for a keyword
3. Show the most common error message
4. Export summary to CSV
5. Quit
Choose an option (1-5): 4

Report filename to write (e.g. summary.csv): summary.csv
Summary written to 'summary.csv'.

1. Show counts by log level
2. Search for a keyword
3. Show the most common error message
4. Export summary to CSV
5. Quit
Choose an option (1-5): 5

Goodbye!

Notice how much of this chapter shows up in one small program: a with block reading the log line by line, a try/except FileNotFoundError guarding the initial open, plain-Python counting logic from earlier chapters, and csv.writer producing a clean report at the end. That's the entire point of this project — file handling isn't just "read a string," it's the foundation real programs use to remember data and turn raw text into a structured report someone else's tool (a spreadsheet, another script) can pick up directly.

What you're building A function library built around file handling and CSV export — load_log_lines, count_by_level, search_lines, most_common_error, export_summary, and print_level_counts — wired to a while True menu loop with five options: show counts by log level, search for a keyword, show the most common error message, export a summary to CSV, or quit. The log file is loaded once at startup behind a try/except FileNotFoundError, the same defensive pattern earlier projects have used, just now organized around reading and processing a real file instead of a data structure or module.
How to run This project reads server.log using a plain relative path, so you must cd into this project/ folder before running it. Download starter.py and server.log below, place them in the same folder, then run python3 starter.py from inside it.

The function library

Ideas to make it your own

Why this project matters

A DevOps engineer scanning last night's server logs for the first sign of trouble, a support team pulling every log line that mentions a specific customer's account, a monitoring script that emails a daily summary of what went wrong — all of these are, underneath, the exact same shape as this project: read a real file defensively, count and search through it with plain logic, and turn the result into something durable and shareable. That's the real skill this project practices — treating a raw text file as a genuine source of truth, and csv as the universal hand-off format between your script and every spreadsheet, database, or dashboard tool that comes after it.

More project ideas

From Chapter 7 onward, you get a genuine choice of what to build. The Log-File Analyzer above is fully built out with a starter and reference solution — the four ideas below are not. Each is a real, grounded use case solvable with only what's been taught through Chapter 13 (everything through Chapter 12's exception-handling material, plus open()/with file I/O and import csv). No starter or solution files are provided on purpose — building one of these unassisted is the point.

1 CSV Contact List Manager

Problem: A phone's contacts app, a small business's client list, a club's membership roster — all need to persist between runs, so the data a user enters today is still there the next time the program starts, not wiped out the moment it closes.

What it should do: Store contacts (name, phone, email) in a CSV file using csv.DictReader/csv.DictWriter, loading the whole file into a list of dicts when the program starts and rewriting the whole file any time a contact is added, updated, or removed. Menu options: list all contacts, add a new contact, search by name, remove a contact by name, and quit (saving before exit). Guard the initial load with try/except FileNotFoundError so a brand-new contact list (no file yet) starts empty instead of crashing.

Suggested approach: Keep the full contact list as a list of dicts in memory during the session, and write a save_contacts(contacts, filename) function using csv.DictWriter with fieldnames=["name", "phone", "email"] that gets called after every change, so the file on disk always matches what's in memory.

2 Persistent To-Do List

Problem: A to-do list that forgets every task the moment the program closes isn't useful for planning anything longer than a single sitting — real to-do apps need tasks to still be there tomorrow.

What it should do: Store tasks as plain lines in a text file (one task per line, perhaps with a done/pending marker at the start of the line), loading them into a list when the program starts and rewriting the whole file whenever a task is added, completed, or removed. Menu options: list tasks (showing done vs. pending clearly), add a task, mark a task done by number, remove a task by number, and quit.

Suggested approach: A simple line format like "[ ] Buy milk\n" or "[x] Buy milk\n" makes both reading and rewriting straightforward with plain string methods (.startswith("[x]"), slicing off the marker) — csv isn't required here since each line only needs one piece of structured information alongside free text.

3 CSV Grade-Report Generator

Problem: A teacher tracking a class's scores across several assignments needs an easy way to turn raw scores into a summary report — each student's average, and who's above or below a passing threshold — without doing the arithmetic by hand every time a new score comes in.

What it should do: Read a CSV file of student scores (one row per student, one column per assignment) using csv.DictReader. Compute each student's average score, flag anyone below a passing threshold (e.g. 60), and export a summary CSV with columns for student name, average, and pass/fail status using csv.DictWriter. Guard the input file's read with try/except FileNotFoundError, printing a clear message if the input file isn't found yet.

Suggested approach: Loop over each row from csv.DictReader, pull out every column except the name, convert each to float, and average them with sum(...) / len(...) — the same running-total logic from Chapter 9's math material, just fed by CSV rows instead of typed input.

4 Word-Frequency Counter

Problem: A writer wanting to know which words they overuse, a search engine ranking how relevant a document is to a query, a spam filter looking for suspicious word patterns — all rely on the same basic tool: counting how often each word appears in a body of text.

What it should do: Read a plain text file, split its contents into individual words (lowercased, with basic punctuation stripped), count how many times each distinct word appears, and export the results as a CSV report (columns: word, count) sorted from most to least frequent, using csv.writer. Menu options: load a text file, show the top N most common words, look up how many times one specific word appears, and export the full report to CSV.

Suggested approach: .lower().split() on the file's full text gets a rough word list; stripping simple punctuation off each word (a loop that removes any of .,!?"';: from both ends) cleans it up enough for this chapter's tools. A dict built with counts[word] = counts.get(word, 0) + 1 tracks frequency, the same counting pattern this project's own most_common_error uses.

← Interview Questions All Chapters →