Python Central HubSign in

Automation and Scripting / Phase 1: OS & File System Automation

The Power of Scripting - Why Automate?

Understand what automation is, when it helps, and how to build safe scripts that save time.

What automation means

Automation is writing code that:

  • performs repetitive tasks reliably
  • reduces human error
  • creates repeatable workflows

Good targets:

  • organizing files
  • backups
  • cleaning data
  • generating reports

What makes a good automation script

  • Idempotent when possible (safe to run twice)
  • Has clear inputs/outputs
  • Has logging
  • Has a dry-run mode

A simple example: generate folders

Python
from pathlib import Path
 
 
def ensure_structure(base: Path):
    for name in ["input", "output", "logs"]:
        (base / name).mkdir(parents=True, exist_ok=True)
 
 
ensure_structure(Path("./my_job"))

Reading the example

ensure_structure builds the same three folders every time. The line that matters is mkdir(parents=True, exist_ok=True). Without exist_ok, the second run raises FileExistsError. With it, the second run changes nothing. That is idempotence: running the script twice gives the same result as running it once. It is the property that lets you schedule a script and stop worrying about whether it already ran.

When automation is worth it

A quick rule: if you do a task more than a few times, and the steps are the same each time, it is a candidate. Estimate the time it saves before you start.

  • A five-minute task done daily costs about 30 hours a year. A script that takes two hours to write pays for itself in a month.
  • A task done once, with steps that change every time, is usually faster by hand.
  • A task where a mistake is expensive, such as sending money or deleting data, needs extra care. Automate it only with checks and a dry run.

The shape of most scripts

Almost every automation script follows the same three steps: read input (files, a web page, a spreadsheet), transform it (clean, filter, calculate), and write output (a file, a message, a report). Keeping those steps in separate functions makes each easy to test and change. The rest of this course teaches the reading and writing tools for different sources.

Common mistakes

  • Automating before understanding. If you cannot do the task by hand and describe each step, you cannot script it.
  • Hardcoding paths, passwords, and dates. Pass them as arguments or read them from environment variables.
  • No logging. When a script runs unattended, the log is the only way to learn what happened.
  • Skipping the dry run. Print what you would do first, and only then do it.
  • Never revisiting scripts. A script that quietly stops working is worse than none. Have it tell you when it fails.

Safety checklist

  • test on a sample folder
  • print actions first
  • keep backups
  • avoid hardcoding absolute delete paths

Check your understanding

quizCheck yourself
  1. What does it mean for a script to be idempotent?

    Show answer

    B — Running it twice gives the same result as running it once — An idempotent script is safe to repeat. mkdir with exist_ok=True is an example: the second call changes nothing.

  2. A task takes 5 minutes, always has the same steps, and you do it every day. What should you do?

    Show answer

    B — Consider automating it, since it repeats and costs about 30 hours a year — Repetition with fixed steps is the best case for automation. Five minutes a day over roughly 365 days is about 30 hours.

  3. Why print the planned actions before performing them?

    Show answer

    B — A dry run lets you catch mistakes before anything is changed or deleted — Reading a list of planned actions costs seconds. Recovering from wrongly moved or deleted files can cost hours, or be impossible without a backup.

Was this page helpful?

Let us know how we did