DEV Community

Art Baker
Art Baker

Posted on

5 Python Scripts That Replaced My Manual Workflows

I used to do these 5 things manually. Now I have scripts for all of them.

1. Bulk File Renaming

Before: rename files one by one in Finder.
After: 3 lines of Python.

import re, pathlib

def bulk_rename(dir, pattern, replacement):
    for f in pathlib.Path(dir).iterdir():
        if f.is_file():
            new = re.sub(pattern, replacement, f.name)
            if new != f.name:
                f.rename(f.parent / new)
Enter fullscreen mode Exit fullscreen mode

2. Merging CSVs

Before: copy-paste between spreadsheets for 30 minutes.
After: one function call.

import pandas as pd, glob

def merge_all(directory):
    files = glob.glob(f"{directory}/*.csv")
    return pd.concat([pd.read_csv(f) for f in files], ignore_index=True)
Enter fullscreen mode Exit fullscreen mode

3. Finding Duplicate Files

Before: manually comparing file sizes and names.
After: MD5 hash every file, group matches.

import hashlib, pathlib
from collections import defaultdict

def find_dupes(dir):
    hashes = defaultdict(list)
    for f in pathlib.Path(dir).rglob('*'):
        if f.is_file():
            hashes[hashlib.md5(f.read_bytes()).hexdigest()].append(f)
    return {h: p for h, p in hashes.items() if len(p) > 1}
Enter fullscreen mode Exit fullscreen mode

4. Caching API Calls

Before: hammering the same API endpoint during development.
After: decorator that caches to disk.

import json, hashlib, os, time
from functools import wraps

def cache_api(ttl=3600):
    os.makedirs('.cache', exist_ok=True)
    def deco(fn):
        @wraps(fn)
        def wrap(*a, **kw):
            key = hashlib.md5(str((a,kw)).encode()).hexdigest()
            path = f'.cache/{key}.json'
            if os.path.exists(path) and time.time() - os.path.getmtime(path) < ttl:
                return json.load(open(path))
            r = fn(*a, **kw)
            json.dump(r, open(path, 'w'))
            return r
        return wrap
    return deco
Enter fullscreen mode Exit fullscreen mode

5. Watching a Folder for New Files

Before: refreshing Finder and manually checking.
After: watchdog triggers a callback.

from watchdog.observers import Observer
from watchdog.events import FileSystemEventHandler
import time

class Handler(FileSystemEventHandler):
    def on_created(self, event):
        if not event.is_directory:
            print(f"New: {event.src_path}")
            # process file here

observer = Observer()
observer.schedule(Handler(), '/path/to/watch')
observer.start()
Enter fullscreen mode Exit fullscreen mode

All 5 of these (plus 5 more) are in my automation toolkit. Each under 100 lines, standalone, documented.

Free preview (3 scripts): github.com/Devtoolkit26/python-automation-toolkit-lite

Full pack: payhip.com/b/QdBYS ($12)

Also: Regex Cookbook ($9) | AI Prompt Pack ($9) | Cheat Sheet ($5)

Top comments (0)