Python for scripts and automation

The default language for getting a computer to do a boring job for you.

Not every backend project is a website. Renaming a thousand files, pulling numbers from an API into a spreadsheet every morning, checking a page for changes, tidying a database - that's automation, and Python is the language most people reach for.


Who gets this pick

You'll see this result when you choose scripts or automation as what you're building and you're not on shared hosting. (On shared hosting the answer is PHP, run from cron - see the PHP pick.)


Why Python fits

Python's standard library covers most automation without installing anything: pathlib and shutil for files and folders, csv and json for data, datetime for dates, subprocess for running other programs, urllib for basic web requests. When you need more, mature packages are a pip install away: requests or httpx for APIs, openpyxl and pandas for spreadsheets, Beautiful Soup for HTML, Playwright for driving a real browser.

The syntax matters too. Automation scripts are often written once and read again months later when something breaks; Python's readability makes that second visit much less painful. And no web framework is needed - a script is just a .py file.


A first script: tidy your downloads folder

from pathlib import Path
import shutil

downloads = Path.home() / "Downloads"

for item in downloads.iterdir():
    if item.is_file():
        folder = downloads / (item.suffix.lstrip(".").lower() or "other")
        folder.mkdir(exist_ok=True)
        shutil.move(str(item), str(folder / item.name))
        print(f"Moved {item.name} -> {folder.name}/")

That's a complete, useful program. Improve it step by step: add a --dry-run flag with argparse that only prints what it would do, handle files that already exist in the destination, and sort by month using each file's modification date.


Next projects

  1. API to CSV. Fetch data from a free public API (weather, exchange rates, your GitHub repositories) and append it to a CSV file.
  2. Spreadsheet report. Read a CSV, summarise it, and write a formatted Excel file with openpyxl.
  3. Change monitor. Check a web page or API once an hour and email yourself when something changes.

Running scripts on a schedule

  • Your own machine: cron on macOS and Linux, Task Scheduler on Windows. Free, but only runs while the computer is on.
  • GitHub Actions: a workflow with a schedule trigger can run a Python script in the cloud on a cron schedule - handy for small jobs that write results back to a repository.
  • A small VPS: the most flexible option, with cron and full control.
  • Platform services: Render, Railway and others offer scheduled jobs, typically on paid plans.

Mistakes to avoid

  • Installing packages globally. Create a virtual environment per project (python -m venv .venv) and record dependencies in requirements.txt.
  • Hard-coding passwords and API keys. Read them from environment variables instead.
  • Destructive scripts without a dry run. Anything that moves, renames or deletes should be able to show you what it will do first.
  • Silent failures. Scheduled scripts run unattended - log what happened to a file and catch errors so you find out when they stop working.
  • Scraping without checking. Read a site's terms and robots.txt, prefer an official API, and don't hammer servers.

When a script needs a web page

The moment you want a form to trigger your script, or a page to show its results, you're building a web app - and Flask lets you wrap the Python you've already written in a few routes. See the Python (Flask) pick.


Take the quiz All picks