Automate Everyday Tasks with Python

Published on: July 10, 2026
Reading time: 5 minutes
representação de automação com ícones de engrenagens, gráficos e pessoas no background

Learning to automate tasks with Python can save hours of repetitive work. A script can organize downloads, rename hundreds of files, process spreadsheets, create reports, monitor folders, and run routine checks with consistent results.

Automation does not require a large application. Many useful scripts are fewer than 100 lines. The key is to choose a repetitive, predictable task, define clear rules, and test the script safely before allowing it to change real files.

What makes a task suitable for automation?

A strong automation candidate is repeated often, follows stable rules, works with digital data, and consumes more time than judgment. Examples include:

  • moving files according to their extensions;
  • renaming photos or documents in a standard format;
  • combining CSV reports;
  • extracting values from text files;
  • generating recurring summaries;
  • checking whether expected files arrived;
  • backing up selected folders.

Tasks involving sensitive decisions, irreversible actions, or frequently changing websites need more safeguards and human review.

Plan before writing code

Describe the current manual process in plain language:

  1. Where does the input come from?
  2. What rules determine each action?
  3. What output should be created?
  4. What can go wrong?
  5. How can the result be checked?
  6. How can changes be reversed?

This planning step separates business rules from Python syntax. The guide to programming logic with Python shows how algorithms turn a process into precise steps.

Use pathlib for files and folders

The standard pathlib module provides object-oriented file paths:

from pathlib import Path

folder = Path.home() / "Downloads"

for path in folder.iterdir():
    print(path.name)

It works across Windows, macOS, and Linux without manually joining path separators. The dedicated guide to pathlib in Python covers creation, navigation, filtering, and file metadata.

Project 1: organize a downloads folder

The following script moves files into category folders based on extension:

from pathlib import Path
import shutil

SOURCE = Path.home() / "Downloads"

CATEGORIES = {
    ".pdf": "Documents",
    ".docx": "Documents",
    ".xlsx": "Spreadsheets",
    ".csv": "Spreadsheets",
    ".jpg": "Images",
    ".jpeg": "Images",
    ".png": "Images",
    ".zip": "Archives",
}

def unique_destination(folder, filename):
    destination = folder / filename
    counter = 1

    while destination.exists():
        destination = folder / f"{destination.stem}_{counter}{destination.suffix}"
        counter += 1

    return destination

for path in SOURCE.iterdir():
    if not path.is_file():
        continue

    category = CATEGORIES.get(path.suffix.casefold(), "Other")
    target_folder = SOURCE / category
    target_folder.mkdir(exist_ok=True)

    destination = unique_destination(target_folder, path.name)
    shutil.move(str(path), destination)
    print(f"Moved: {path.name} -> {destination.name}")

The duplicate-name function prevents silent overwrites. Run this script against a test directory before using the real Downloads folder.

The official shutil documentation describes high-level copy, move, archive, and directory operations.

Add a dry-run mode

A dry run shows intended changes without applying them:

DRY_RUN = True

if DRY_RUN:
    print(f"Would move {path} to {destination}")
else:
    shutil.move(str(path), destination)

This simple switch is one of the most valuable safety features in file automation.

Project 2: rename files in batches

from pathlib import Path

folder = Path("photos")
files = sorted(folder.glob("*.jpg"))

for index, path in enumerate(files, start=1):
    new_name = f"vacation_{index:03d}{path.suffix.lower()}"
    destination = path.with_name(new_name)

    if destination.exists():
        raise FileExistsError(f"Already exists: {destination}")

    path.rename(destination)
    print(f"{path.name} -> {destination.name}")

The format {index:03d} produces 001, 002, and so on, preserving correct alphabetical order.

Before renaming, create a mapping file that records old and new names. That log can support a rollback script.

Project 3: summarize CSV data

Python’s standard csv module is enough for many reports:

import csv
from collections import defaultdict
from pathlib import Path

source = Path("sales.csv")
totals = defaultdict(float)

with source.open(encoding="utf-8", newline="") as file:
    reader = csv.DictReader(file)

    for row in reader:
        category = row["category"].strip()
        amount = float(row["amount"])
        totals[category] += amount

with Path("summary.csv").open("w", encoding="utf-8", newline="") as file:
    writer = csv.writer(file)
    writer.writerow(["category", "total"])

    for category, total in sorted(totals.items()):
        writer.writerow([category, f"{total:.2f}"])

The CSV files guide covers delimiters, dictionaries, quoting, and Pandas alternatives.

Project 4: create a backup

from datetime import datetime
from pathlib import Path
import shutil

source = Path.home() / "Documents" / "project"
backup_root = Path.home() / "Backups"
backup_root.mkdir(exist_ok=True)

timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
archive_base = backup_root / f"project_{timestamp}"

archive_path = shutil.make_archive(
    str(archive_base),
    "zip",
    root_dir=source,
)

print(f"Backup created: {archive_path}")

Backups should be tested by restoring a sample file. A ZIP existing on disk does not prove that it contains the expected data.

The article on automatic file backups adds retention rules and more validation.

Separate configuration from code

Hard-coded paths and credentials make scripts difficult to reuse. Store non-secret settings in JSON or command-line arguments:

{
  "source_folder": "incoming",
  "archive_folder": "processed",
  "extensions": [".csv", ".xlsx"]
}
import json
from pathlib import Path

with open("config.json", encoding="utf-8") as file:
    config = json.load(file)

source = Path(config["source_folder"])

For secrets, use environment variables instead of committed files. The guide to environment variables in Python covers safe configuration practices.

Add logging

Terminal messages disappear when a scheduled script runs unattended. The standard logging module creates a durable record:

import logging

logging.basicConfig(
    filename="automation.log",
    level=logging.INFO,
    format="%(asctime)s %(levelname)s %(message)s",
)

logging.info("Automation started")

Log meaningful events: start and finish, number of files processed, skipped items, errors, and output locations. Avoid logging passwords or private data. The Python logging guide covers levels, handlers, and formatting.

Handle errors per item

One bad file should not always stop an entire batch:

for path in files:
    try:
        process_file(path)
    except PermissionError:
        logging.exception("Permission denied: %s", path)
    except ValueError:
        logging.exception("Invalid data: %s", path)

Catch specific exceptions and decide whether to skip, retry, quarantine, or stop. Broad exception handling can hide programming bugs.

Make scripts idempotent

An idempotent automation can run again without corrupting data or duplicating work. Common techniques include:

  • moving completed files to a processed folder;
  • checking whether output already exists;
  • using unique record identifiers;
  • writing temporary output and replacing the final file only after success;
  • recording processed items in a state file.

Schedule automation

On Windows, Task Scheduler can run a script at a chosen time. On Linux and macOS, cron is a common option. Use the full path to the virtual environment’s Python executable and the script file because scheduled jobs may start in a different working directory.

/path/to/project/.venv/bin/python /path/to/project/main.py

Build paths from the script location rather than assuming a current directory:

from pathlib import Path

BASE_DIR = Path(__file__).resolve().parent
CONFIG_FILE = BASE_DIR / "config.json"

Test automation safely

  1. Use copies of real data.
  2. Start with a tiny batch.
  3. Enable dry-run mode.
  4. Record every planned operation.
  5. Test duplicate names and missing files.
  6. Interrupt the script midway and verify recovery.
  7. Confirm outputs before deleting inputs.

Reusable processing functions are easier to test than one large script. The guide to unit tests in Python shows how to validate behavior without modifying real folders.

Common mistakes

  • Testing directly on important files.
  • Overwriting duplicate filenames.
  • Using relative paths that depend on the terminal location.
  • Deleting inputs before output validation.
  • Embedding passwords in source code.
  • Ignoring encoding and delimiter differences.
  • Scheduling a script without logs or alerts.
  • Automating an unstable process before understanding it.

A maintainable project structure

automation_project/
├── config.json
├── main.py
├── operations.py
├── validation.py
├── logs/
├── tests/
└── README.md

Place file operations, validation, and configuration in separate modules. The guide to Python modules and packages explains how this organization supports reuse.

Conclusion

Python automation works best when the task is repetitive, rules are explicit, and failures can be detected. Start with a safe script that reads information and reports what it would do. Add a dry-run mode, logs, specific error handling, duplicate protection, and a recovery plan before enabling real changes.

A file organizer, batch renamer, CSV summary, or timestamped backup is an excellent first project. Each one develops skills that transfer to larger workflows involving spreadsheets, email, APIs, and databases.

Share:

Facebook
WhatsApp
Twitter
LinkedIn

Article content

    Related articles

    logo do BeautifulSoup em um fundo branco
    Automation and Scripts
    Foto de perfil de Leandro Hirt da Academify

    Web Scraping with BeautifulSoup and Requests

    Learn web scraping with Requests and BeautifulSoup: fetch pages, parse HTML, select elements, clean data, follow pagination, handle errors, respect

    Ler mais

    Tempo de leitura: 6 minutos
    10/07/2026
    Criação de bot Telegram com Python
    Automation and Scripts
    Foto de perfil de Leandro Hirt da Academify

    Build a Telegram Bot with Python

    Build a Telegram bot with Python step by step: create a bot token, handle commands and messages, add buttons, store

    Ler mais

    Tempo de leitura: 5 minutos
    10/07/2026
    Automação web com Selenium usando Python
    Automation and ScriptsWeb Development
    Foto de perfil de Leandro Hirt da Academify

    Selenium with Python: Complete Web Automation Guide

    Learn Selenium with Python from setup to browser control, element locators, waits, forms, screenshots, page objects, testing, and reliable web

    Ler mais

    Tempo de leitura: 5 minutos
    10/07/2026
    Geração de arquivos PDF usando biblioteca FPDF em Python
    Automation and Scripts
    Foto de perfil de Leandro Hirt da Academify

    Create PDF Files with Python and FPDF

    Learn to create PDF files with Python and fpdf2: pages, fonts, paragraphs, images, tables, headers, footers, Unicode, invoices, paths, and

    Ler mais

    Tempo de leitura: 4 minutos
    10/07/2026
    Logos do Python e Excel lado a lado representando a importação de dados do Excel para Python.
    Automation and Scripts
    Foto de perfil de Leandro Hirt da Academify

    Import Excel Data into Python with Pandas

    Learn how to import Excel data into Python with Pandas and openpyxl, select sheets and columns, clean values, handle dates

    Ler mais

    Tempo de leitura: 4 minutos
    10/07/2026
    Python e ícone de e-mail sobre teclado de notebook
    Automation and Scripts
    Foto de perfil de Leandro Hirt da Academify

    Automate Emails with Python: Complete Guide

    Learn how to automate emails with Python using EmailMessage and smtplib, environment variables, HTML, attachments, multiple recipients, scheduling, and error

    Ler mais

    Tempo de leitura: 5 minutos
    10/07/2026