A Beginner's Guide to Python Automation (Scripts You Can Actually Copy)
Most "learn Python automation" articles hand you a wall of theory and one toy example that doesn't do anything you'd actually want. This is the opposite. Below are four small scripts I genuinely reach for, each solving a boring real task, each short enough to read in one sitting and copy straight into a file. I write software for a living, and a surprising amount of what saves me time isn't clever — it's fifteen lines that do a tedious thing perfectly every time.
I'll also be honest at the end about where automating something costs more than it saves, because "just automate it" is not always the right answer.
How to actually run a Python script
If you've never run one, the whole loop is: install Python, save a .py file, run it from a terminal. Concretely:
- Install Python 3 from python.org (or your system's package manager). On macOS and Linux you'll usually type
python3; on Windows,python. - Save the code into a file, for example
organize.py. - Open a terminal in that folder and run
python3 organize.py.
Two scripts below need extra libraries. Install those with pip, Python's package manager:
pip install requests beautifulsoup4 Pillow
If you want to keep a project's libraries isolated (a good habit, not required to start), create a virtual environment first with python3 -m venv venv and activate it. That's genuinely all the setup you need.
One safety note before you run anything that moves or deletes files: test on a copy of a folder first. A script does exactly what you tell it, instantly, with no undo.
Script 1: Organize a messy folder by file type
Every Downloads folder eventually becomes a swamp. This sorts files into subfolders by type — images together, documents together, archives together.
from pathlib import Path
# The folder to organize — change this to whatever you want
target = Path.home() / "Downloads"
# Which extensions go into which subfolder
categories = {
"images": [".jpg", ".jpeg", ".png", ".gif", ".webp"],
"documents": [".pdf", ".docx", ".txt", ".xlsx", ".pptx"],
"archives": [".zip", ".tar", ".gz", ".rar"],
}
for file in target.iterdir():
if not file.is_file():
continue
for folder_name, extensions in categories.items():
if file.suffix.lower() in extensions:
destination = target / folder_name
destination.mkdir(exist_ok=True)
file.rename(destination / file.name)
break
Path from the built-in pathlib module is the modern way to handle files — no extra install needed. The script skips folders, matches each file's extension against your categories, creates the destination subfolder only if needed, and moves the file. Change target and the categories map to fit your life.
Script 2: Scrape a table from a web page into a CSV
This grabs the first HTML table on a page and writes it to a spreadsheet-ready CSV. This is the one that replaces the miserable job of copying rows by hand.
import csv
import requests
from bs4 import BeautifulSoup
url = "https://example.com/page-with-a-table"
response = requests.get(url, timeout=10)
response.raise_for_status() # stop early if the page failed to load
soup = BeautifulSoup(response.text, "html.parser")
table = soup.find("table")
with open("output.csv", "w", newline="", encoding="utf-8") as f:
writer = csv.writer(f)
for row in table.find_all("tr"):
cells = [cell.get_text(strip=True) for cell in row.find_all(["th", "td"])]
writer.writerow(cells)
print("Saved output.csv")
It needs requests (to fetch the page) and beautifulsoup4 (to read the HTML) from the install command above. raise_for_status() makes it fail loudly on a bad URL instead of silently writing garbage.
One responsible-adult note: scraping isn't a free-for-all. Check the site's terms of service and its robots.txt, don't hammer a server with rapid repeated requests, and never republish data you're not allowed to. Scrape public data you have a right to use, gently.
Script 3: Send yourself an email (report, reminder, alert)
Automations get useful when they can tell you something happened. This sends an email using Python's built-in smtplib — no libraries to install.
import smtplib
from email.message import EmailMessage
msg = EmailMessage()
msg["Subject"] = "Automated report"
msg["From"] = "you@example.com"
msg["To"] = "you@example.com"
msg.set_content("This message was sent automatically by a Python script.")
# Use your email provider's SMTP server and an app-specific password,
# NOT your normal account password.
with smtplib.SMTP_SSL("smtp.example.com", 465) as server:
server.login("you@example.com", "your-app-password")
server.send_message(msg)
print("Email sent")
The important detail: most providers no longer let a script log in with your regular password. You generate an app-specific password in your email account's security settings and use that. For Gmail, the server is smtp.gmail.com on port 465 (could vary); other providers publish their own SMTP host and port.
Making it "scheduled" is a separate step from the script itself. You don't loop the script forever — you let your operating system run it on a timer. On macOS or Linux, cron runs a command on a schedule; on Windows, Task Scheduler does the same. Point either one at python3 send_report.py and pick a time. That's how a script becomes a daily 8 a.m. report instead of something you remember to run.
Script 4: Batch-resize a folder of images
If you've ever needed to shrink fifty photos before uploading them, this does the whole folder at once. It uses Pillow, the standard Python imaging library (pip install Pillow).
from pathlib import Path
from PIL import Image
source = Path("photos")
output = Path("photos-resized")
output.mkdir(exist_ok=True)
MAX_WIDTH = 1200 # resize anything wider than this, keep the aspect ratio
for image_path in source.glob("*.jpg"):
with Image.open(image_path) as img:
if img.width > MAX_WIDTH:
ratio = MAX_WIDTH / img.width
new_height = round(img.height * ratio)
img = img.resize((MAX_WIDTH, new_height))
img.save(output / image_path.name)
print("Done resizing")
It reads every .jpg in a photos folder, scales down anything wider than 1200 pixels while keeping the proportions correct, and writes the results to a separate photos-resized folder so your originals are never touched. Change *.jpg to *.png for PNGs, or the MAX_WIDTH for a different size.
Where automation is worth it — and where it isn't
Here's the part the "automate everything" crowd leaves out. Automation pays off when a task is repetitive, rule-based, and frequent — the same steps, the same logic, over and over. All four scripts above qualify: the rules never change, so a script does them perfectly forever.
Automation is a bad deal when:
- It's a one-off. If you'll do it once, doing it by hand is faster than writing, testing, and debugging a script. Time spent automating is only repaid by repetition.
- The task needs judgment. "Rename these files by type" automates cleanly. "Pick the best photo from each set" does not — the moment a human has to decide, a script becomes fragile.
- The inputs keep changing shape. A scraper written for one page layout breaks the day the site is redesigned. Automating something unstable means signing up to maintain it forever.
The honest rule of thumb: if a boring task shows up on your plate for the third time and it follows the same steps every time, it's a candidate. If you're automating to avoid a task you'll only face once, you're procrastinating with extra steps — and I say that as someone who has absolutely done it.
Start with the script closest to a problem you actually have this week. Change the folder paths and the values at the top, run it on a copy, and watch it do in a second what used to take twenty minutes. That first small win is what makes the rest click.