Stage 3 of 6

Files, Automation & CLI

Stage 03 — Files, Automation, and Robust CLI Programs

Reading, Writing, and Processing Files Like a Pro

Welcome to Stage 03. In Stage 02 you learned how to structure logic with functions and work with data in memory (lists/dicts). In this stage you take the next major step: working with data stored in files and building professional command-line tools with arguments, error handling, and meaningful exit codes. :contentReference[oaicite:1]{index=1}

By the end of this stage, you will build a Log Analyzer — a practical cybersecurity-style CLI utility that reads logs, summarizes events, and optionally exports results to JSON.


What You Will Learn

You will gain hands-on skill in:


What You Will Build

A Log Analyzer command-line tool that:


Recommended Folder Layout

Inside stage_03_Files_Automation_CLI/:

stage_03_Files_Automation_CLI/
├── README.md
├── main.py
├── data/
│   ├── sample.txt
│   ├── sample.log
│   └── config.json
└── output/
    └── (generated files go here)

Notes:

Milestones (Complete in Order)

Milestone 1: Read your first file and display its contents

Milestone 2: Write data to a new file

Milestone 3: Use pathlib to handle file paths properly

Milestone 4: Read and write JSON data

Milestone 5: Create a CLI with argparse (--help, --input, --output)

Milestone 6: Build Log Analyzer Part A (read and count log entries)

Milestone 7: Build Log Analyzer Part B (full tool with JSON export)

Milestone 8: Add exit codes and push to GitHub

Create Your Sample Data Files

1) data/sample.txt

Create data/sample.txt with:

Hello, this is my first text file!
Python can read every line.
This is line 3.
And this is the last line.

2) data/sample.log

Create data/sample.log with:

2024-01-15 08:00:00 INFO Application started
2024-01-15 08:01:00 INFO User admin logged in
2024-01-15 08:02:00 WARNING Disk space low
2024-01-15 08:03:00 ERROR Database timeout
2024-01-15 08:04:00 INFO Backup completed
2024-01-15 08:05:00 WARNING High memory usage
2024-01-15 08:06:00 ERROR Network unreachable
2024-01-15 08:07:00 INFO User admin logged out

3) data/config.json (optional practice file)

Create data/config.json with:

{
  "app_name": "Security Scanner",
  "version": "1.0.0",
  "max_threads": 4,
  "verbose": true
}

How to Run (Log Analyzer)

From inside stage_03_Files_Automation_CLI/:

Show help

python main.py --help

Analyze the sample log (no JSON output)

python main.py --input data/sample.log

Analyze with verbose output

python main.py --input data/sample.log --verbose

Analyze and export JSON report

python main.py --input data/sample.log --output output/report.json

Expected Output (Example)

When you run:

python main.py --input data/sample.log

You should see a short summary similar to:

Success Checklist

You are “done” with Stage 03 when you can confidently say:

Git Completion (This is a MUST!)

git status
git add stage_03_Files_Automation_CLI
git commit -m "Complete Stage 03 - Files, CLI, Log Analyzer"
git push

Next Stage Preview

Stage 04 will move into networking and APIs (HTTP requests, timeouts, combining local + remote data).

See you in the next stage. Keep up the good work!

← Previous Stage 3 of 6 Next →