On this page

Guided Exercise: Build a Full Automation Script Suite

Enrico Hochkoeppler Profile

By Enrico Hochkoeppler

Web Developer & TutoriaLibre Founder

Overview of the exercise​

This guided exercise brings together the core skills developed in this module: filesystem automation, text processing, web requests, scheduling, and CLI tool design. You will build a cohesive automation suite composed of multiple Python modules, each responsible for a specific task. The suite will be operated through a command‑line interface and will run scheduled jobs that interact with both local files and remote resources.

The goal is to design a professional‑grade automation system that is modular, maintainable, and easy to extend. Each component must be implemented using only Python’s standard library.

Project architecture​

A well‑structured automation suite separates responsibilities into clear modules. A typical layout for this exercise is:


automation_suite/

    cli.py

    files.py

    textproc.py

    web.py

    scheduler.py

    utils/

        logging.py

Each module focuses on a single domain:

This structure ensures clarity and supports long‑term maintainability.

Implementing filesystem automation​

The filesystem module provides functions for scanning directories, organizing files, and creating backups. It should use pathlib, shutil, and safe automation patterns.

Example: organizing files by extension.


from pathlib import Path

import shutil



def organize_by_extension(directory):

    directory = Path(directory)

    for item in directory.iterdir():

        if item.is_file():

            ext = item.suffix.lower().lstrip(".") or "no_extension"

            target = directory / ext

            target.mkdir(exist_ok=True)

            shutil.move(str(item), str(target / item.name))

This function becomes part of the suite’s available commands.

Implementing text‑processing automation​

The text‑processing module provides reusable functions for cleaning, transforming, and extracting information from text files.

Example: cleaning a text file.


from pathlib import Path



def clean_text_file(path):

    path = Path(path)

    content = path.read_text(encoding="utf-8")

    cleaned = content.strip().replace("\t", " ").lower()

    path.write_text(cleaned, encoding="utf-8")

This module can also include pattern extraction using re, batch processing, and structured parsing.

Implementing web‑request automation​

The web module handles downloading files, fetching remote text, and interacting with simple HTTP endpoints using urllib.

Example: downloading a remote resource.


from urllib.request import urlopen

from pathlib import Path



def download(url, target_path):

    with urlopen(url) as response:

        data = response.read()

    target = Path(target_path)

    target.parent.mkdir(exist_ok=True)

    target.write_bytes(data)

This function integrates with the filesystem module to store downloaded data in organized directories.

Implementing a scheduler​

The scheduler module runs tasks at fixed intervals or specific times using time.sleep, datetime, and controlled loops.

Example: running a task every hour.


import time



def run_every(interval_seconds, func, *args):

    while True:

        func(*args)

        time.sleep(interval_seconds)

This module can orchestrate periodic downloads, cleanup routines, or text‑processing jobs.

Building the command‑line interface​

The CLI module ties the entire suite together using argparse. It exposes subcommands that trigger the functions implemented in the other modules.

Example: defining subcommands.


import argparse

from files import organize_by_extension

from textproc import clean_text_file

from web import download



def main():

    parser = argparse.ArgumentParser(description="Automation Suite")

    subparsers = parser.add_subparsers(dest="command")



    org = subparsers.add_parser("organize")

    org.add_argument("directory")



    clean = subparsers.add_parser("clean")

    clean.add_argument("path")



    dl = subparsers.add_parser("download")

    dl.add_argument("url")

    dl.add_argument("target")



    args = parser.parse_args()



    if args.command == "organize":

        organize_by_extension(args.directory)

    elif args.command == "clean":

        clean_text_file(args.path)

    elif args.command == "download":

        download(args.url, args.target)

This interface allows users to run commands such as:


python cli.py organize downloads/

python cli.py clean notes.txt

python cli.py download https://example.com/data.csv data/data.csv

Integrating logging​

A simple logging utility provides timestamped output for all modules.


from datetime import datetime



def log(message):

    print(f"[{datetime.now()}] {message}")

Each module should call this function to record actions, improving traceability and debugging.

Final integration​

Once all modules are implemented, the suite operates as a cohesive system:

This exercise demonstrates how to design a complete automation ecosystem using only Python’s standard library, following professional engineering principles and modular architecture.

Found this helpful? Share the knowledge...

... or support this project to keep it alive!

Buy Me a Coffee

Previous

CLI Tools

Next

Functional Programming