Automation & Workflows

Lesson 4 of 7

Lesson 4 — Scripting for Automation

Write small Python or Bash scripts to rename files, process data and run on a schedule.

🟡 Intermediate 80 XP

Learn it

A script is a list of commands saved in a file that can be run again and again.

Python is friendly for beginners and has libraries for files, dates, web requests and data. Bash is powerful on Linux and Mac for file and system tasks.

Scripts can read files, loop through folders, make decisions with if/else, and write results.

You can schedule scripts with cron (Linux/Mac) or Task Scheduler (Windows) so they run automatically.

Key terms

Script
A saved set of commands that can be run repeatedly.
Loop
Code that repeats while a condition is true or for each item in a collection.
Cron
A Linux/Mac scheduler for running commands at set times.
Environment variable
A named value available to programs, often used for config.
PowerShell
Microsoft's scripting and automation shell.

A file-organising script

Downloads folder is full of mixed files.

  1. 1Scan: List every file in the Downloads folder.
  2. 2Sort: Move images to Pictures, PDFs to Documents, videos to Videos.
  3. 3Log: Write a line to a log file for each move.
  4. 4Schedule: Run the script every Sunday at 20:00.

Python script to sort downloads

pythonimport os, shutil
from pathlib import Path

DOWNLOADS = Path.home() / 'Downloads'
RULES = {'.pdf': 'Documents', '.jpg': 'Pictures', '.png': 'Pictures', '.mp4': 'Videos'}

for file in DOWNLOADS.iterdir():
    if file.is_file():
        folder = RULES.get(file.suffix)
        if folder:
            target = DOWNLOADS / folder
            target.mkdir(exist_ok=True)
            shutil.move(str(file), str(target / file.name))
            print(f'Moved {file.name} to {folder}')

It loops through files, checks the extension, moves matching files and creates folders if needed.

Try it

Complete the loop that greets each name.

names = ['Ana', 'Ben', 'Cara']
for name in names:
  print(______)

Challenge

Write a short Python or Bash script that renames every file in a folder by adding today's date prefix (e.g. report.txt becomes 2026-08-25_report.txt). Include a safety check so it only renames files, not folders.

Pick whichever way suits you — every mode earns the same bonus XP.

Write at least 40 more characters to submit.

Mark your own work

Guided walkthrough — 0/3 clues revealed

  1. Clue 1 locked — reveal it only if you get stuck.
  2. Clue 2 locked — reveal it only if you get stuck.
  3. Clue 3 locked — reveal it only if you get stuck.

Each clue costs 4 XP (never below 20 XP). You'd earn 40 XP right now.

Quiz time

Question 1 of 4Score 0

A script is…