MrFaws Docs
Learn Python

Automation and CLI Tools

Build reliable scripts, command-line tools and scheduled jobs.

Command-line arguments

import argparse

parser = argparse.ArgumentParser(description="Find large files")
parser.add_argument("path")
parser.add_argument("--min-mb", type=float, default=100)
args = parser.parse_args()

A good CLI provides --help, clear errors and meaningful exit codes. Add --dry-run before any tool moves, overwrites or deletes files.

Logging

import logging

logging.basicConfig(
    level=logging.INFO,
    format="%(asctime)s %(levelname)s %(message)s",
)
logger = logging.getLogger(__name__)
logger.info("Job started")

Use logging for operations and print for user-requested output.

Subprocesses

import subprocess

result = subprocess.run(
    ["git", "status", "--short"],
    check=True,
    capture_output=True,
    text=True,
)

Pass arguments as a list and avoid shell=True with user input. Schedule repeatable scripts through cron, systemd timers or Windows Task Scheduler.

Exercise

Build a backup tool with a config file, dry-run mode, checksums, structured logging and a final report.

On this page