Chapter 5 — Python scripting basics
Lesson map
Lesson 05 published. Map, example, EX013.
flowchart TD
N0["Learning outcomes"]
N1["5.1 First scripts"]
N2["5.2 Reading files"]
N3["5.3 Clear output"]
N4["5.4 Safe habits"]
N0 --> N1 --> N2 --> N3 --> N4
N4 --> EX["Worked example"]
EX --> Q["Exercise EX013"]
Logical drawing
flowchart LR
N0["Learning outcomes"]
N1["5.1 First scripts"]
N2["5.2 Reading files"]
N3["5.3 Clear output"]
N4["5.4 Safe habits"]
N0 --> N1 --> N2 --> N3 --> N4 --> Q["EX013"]
[Learning outcomes]
|
v
[5.1 First scripts]
|
v
[5.2 Reading files]
|
v
[5.3 Clear output]
|
v
[5.4 Safe habits]
|
v
[Worked example] --> [Exercise EX013]
Learning outcomes
You will (1) write simple Python scripts for security tasks; (2) read CSV and text files; (3) produce clear, repeatable output.
5.1 First scripts
Jamal learns Python using only the standard library, so scripts run on any machine. His first script reads a list of Riverbend user accounts from a CSV file and counts accounts per group. His second lists accounts that have not logged in for sixty days. Ms. Grant explains that scripting turns slow manual checks into fast, repeatable ones, which is essential for small IT teams responsible for hundreds of devices. He runs both scripts weekly in the laboratory and compares the results with manual counts to confirm they are correct.
5.2 Reading files
Jamal learns to read text and CSV files with the csv module and to handle problems: empty lines, missing columns and unexpected values. His script skips bad lines and reports them, so data problems are visible rather than hidden. He tests it with a file containing deliberate errors. He learns that security scripts must fail loudly, because a script that silently ignores half its input gives false reassurance. His test file contains ten deliberate errors, and the script reports all ten with their line numbers, which gives him confidence that it will not hide problems in real data.
5.3 Clear output
Jamal formats output for people: a heading, the date, totals and a list of items needing action, sorted by importance. He also writes results to a CSV file for the IT manager's records. Ms. Grant asks him to include the script version and input file name in each report, so anyone can trace a result back to exactly what produced it. The IT manager says the reports are easy to read and asks Jamal to use the same format for every script he writes during the placement. Ms. Grant uses one of his reports as an example in class.
5.4 Safe habits
Jamal stores scripts in a version-controlled folder, never writes passwords into them and keeps outputs containing personal data in a restricted folder. He adds comments explaining why each check exists. Ms. Grant notes that scripts often outlive their authors, so clear comments and safe handling of data are part of writing good security tools. He also writes a short README file for each script explaining what it checks, what input it needs and what its output means. Ms. Grant notes that these files make it possible for the IT manager to run the scripts after Jamal leaves.
Worked example
Jamal writes standard-library scripts that count accounts and flag inactivity, handles bad input loudly, formats traceable output and keeps scripts safe.
Synthetic case. Data are synthetic.
Errors and limits
Scripts that silently skip bad input give false reassurance.
Exercises
- EX013 — Inactivity script. Which accounts does the second script list? Rubric: 3 = no login for sixty days;; 2 = partly correct;; 1 = vague;; 0 = none.
- EX014 — Bad lines. How does the script handle bad lines? Rubric: 3 = skips and reports them;; 2 = partly correct;; 1 = ignore;; 0 = none.
- EX015 — Traceability. What does each report include for tracing? Rubric: 3 = script version and input file name;; 2 = partly correct;; 1 = vague;; 0 = none.
Continue studying — lesson 05 (chapter 05 of 30)
← Chapter 04 · Course chapter index · Chapter 06 →
Continue to the next authored lesson.