os, glob, argv and process exit

Does the data folder exist? Which CSV files match *.csv? What did the human type after python app.py? How do you exit with a non-zero status the scheduler can see?

Period timetable

Nested records model a school day.

You will be able to

  • Keep folder names, glob patterns and argv in one door
  • Fail with a clear exit when the data folder is missing
  • Treat stderr as the place ops grep, not a mystery crash dump

A nightly attendance job needs a folder and a name

os tells you about this machine: does data/gate exist, how to join a path so Windows and Linux do not fight. glob answers ‘which files look like *.csv’. sys.argv is the words after python app.py — a label, a date, a dry-run flag. None of that runs in this browser.

You still practise the decisions: a CONFIG dict for the folder, a list of names standing in for glob, a function that returns 2 when the folder flag is missing — the same non-zero exit a scheduler can see. Copy the labelled sample onto a laptop inside a venv.

Do not hard-code C:\Users\that-intern

Paths belong in environment variables or a config file next to the project. Command-line arguments belong in argv, parsed once at the door. stderr is for ‘missing folder’ so the CSV on stdout stays clean.

sys.exit(2) (or raise SystemExit) is how a cron job turns red. Printing a stack trace into a clerk’s WhatsApp is not a policy.

Words that matter

os
Standard library for files, folders and environment.
glob
Match filenames with a pattern such as *.csv.
argv / exit
Launch words, and a numeric status the scheduler can see.

Common mistakes

Avoid: A path that only exists on one intern’s laptop, baked into the file.

Do this: CONFIG or an environment variable, with a non-zero exit if missing.

Run it step by step

Each box is a real program. Press Run, change a number, Run again — the output must follow your code.

1. Step 1 — path and glob analog as config

Change the folder once. The print follows. Names stand in for *.csv.

Python sandboxlesson://workspace
console

Edit the example, press Run, then Build if you want a compile check.

build

Press Build to compile.

2. Step 2 — missing folder is a numbered exit

ERROR on the log line. 2 is what cron would see.

Python sandboxlesson://workspace
console

Edit the example, press Run, then Build if you want a compile check.

build

Press Build to compile.

On a full Python install — os, glob, sys

Nightly attendance: create or check a folder, list CSV files, read a label from the command line, exit non-zero if the folder is missing.

No pip — stdlib.

Door reads argv; stderr + exit for ops; helpers stay testable.

Real library code (not run in this browser sandbox)

import glob
import os
import sys

DATA = os.environ.get("RSIL_DATA", "data/gate")

def csv_paths(folder):
    return glob.glob(os.path.join(folder, "*.csv"))

if __name__ == "__main__":
    if not os.path.isdir(DATA):
        print("missing folder:", DATA, file=sys.stderr)
        raise SystemExit(2)
    label = sys.argv[1] if len(sys.argv) > 1 else "gate"
    print(label, csv_paths(DATA))

Example program — Guard an empty argv label

A missing file name is a usage line, not a crash dump.

Python sandboxlesson://workspace
console

Edit the example, press Run, then Build if you want a compile check.

build

Press Build to compile.

Your turn — Stamp a job

stamp(label, day) returns label-day. Print stamp("lab", "2026-08-24") so lab-2026-08-24 appears.

Python sandboxlesson://workspace
console

Edit the example, press Run, then Build if you want a compile check.

build

Press Build to compile.

Self-assessment

Check your understanding before you mark the lesson complete.

1. sys.argv is for…
2. A missing data folder should usually…

Progress is stored in a browser cookie on this device.