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.
Standard library; zero deps.
import argparsefrom pathlib import Pathparser = 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
pip install click.
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
Drop-in replacement for requests + async support.
import httpx# Syncr = httpx.get("https://api.github.com/users/torvalds")r.raise_for_status()print(r.json()["name"])# With auth + timeoutr = httpx.get("https://api.example.com/v1/me",headers={"Authorization": f"Bearer {token}"},timeout=10.0,)# POST JSONr = 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")
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.
Pick the senior answer.
Sign in and purchase access to unlock this lesson.