█
LastWrite
  • > Curriculum
  • > Pricing
  • > For Educators
  • > About
  • > Contact
Log InGet Started

Questions, concerns, bug reports, or suggestions? We read every message, write to us at [email protected].

More ways to reach us →
LastWrite

Structured computer science lessons for aspiring developers and security professionals.

[email protected]

(201) 785-7951

Mon–Fri, 9 AM–5 PM EST

Learn

  • Curriculum
  • Pricing

Company

  • About
  • For Educators & Schools
  • Contact Us

Legal

  • Terms of Service
  • Privacy Policy
© 2026 LastWrite. All rights reserved.
Curriculum/Programming Languages/Python (Professional)/Python for Scripting and Automation
45 minIntermediate

Python for Scripting and Automation

After this lesson, you will be able to: Build CLI tools with argparse or Click, automate file operations, and call APIs with httpx.

Python's killer use case: scripts that automate real work.

Prerequisites:Testing in Python

CLI with argparse

Standard library; zero deps.

python
import argparse
from pathlib import Path
parser = argparse.ArgumentParser(description="Resize images")
parser.add_argument("src", type=Path, help="source directory")
parser.add_argument("--width", type=int, default=1024)
parser.add_argument("--dry-run", action="store_true")
args = parser.parse_args()
for img in args.src.rglob("*.jpg"):
if args.dry_run:
print(f"would resize {img}")
else:
resize(img, args.width)
# Use: python resize.py ./photos --width 800 --dry-run

CLI with Click (nicer for complex tools)

pip install click.

python
import click
@click.group()
def cli(): pass
@cli.command()
@click.argument("name")
@click.option("--greeting", default="Hello")
def hello(name, greeting):
click.echo(f"{greeting}, {name}")
@cli.command()
@click.argument("src", type=click.Path(exists=True))
@click.option("--dry-run", is_flag=True)
def resize(src, dry_run):
...
if __name__ == "__main__": cli()
# Use: python tool.py hello Alex --greeting Hi
# Use: python tool.py resize ./photos --dry-run

Modern HTTP with httpx

Drop-in replacement for requests + async support.

python
import httpx
# Sync
r = httpx.get("https://api.github.com/users/torvalds")
r.raise_for_status()
print(r.json()["name"])
# With auth + timeout
r = httpx.get(
"https://api.example.com/v1/me",
headers={"Authorization": f"Bearer {token}"},
timeout=10.0,
)
# POST JSON
r = httpx.post("https://api.example.com/users", json={"name": "A"})
# Session (reuse connection, default headers)
with httpx.Client(headers={"User-Agent": "mytool/1.0"}) as client:
r1 = client.get("https://api.example.com/a")
r2 = client.get("https://api.example.com/b")

Common mistakes only experienced Python devs avoid

Hardcoding paths instead of CLI args. Same script can't serve two projects. Skipping --dry-run. First run on the wrong directory = data loss. No timeouts on HTTP calls. Hangs forever when API is slow. Using requests in new code, httpx is the modern choice with both sync + async.

Quick Check

Why include --dry-run on a destructive CLI?

Pick the senior answer.

Sign in and purchase access to unlock this lesson.

Sign in to purchase
←Testing in Python with pytest
Back to Python (Professional)
Python for Data (pandas + NumPy)→