DEV Community

Art Baker
Art Baker

Posted on

5 Python Scripts I Use Every Week (And You Can Too)

Every developer has a collection of scripts they reach for constantly. Here are the 5 I run almost daily — each under 100 lines, zero complex dependencies.

1. Bulk File Renamer

When you need to rename hundreds of files following a pattern — CSV exports, image dumps, log files:

import os, re

def bulk_rename(directory, pattern, replacement):
    for f in os.listdir(directory):
        new_name = re.sub(pattern, replacement, f)
        if new_name != f:
            os.rename(
                os.path.join(directory, f),
                os.path.join(directory, new_name)
            )
            print(f"  {f}{new_name}")
Enter fullscreen mode Exit fullscreen mode

2. CSV Column Merger

Got 5 CSV exports with different column orders? This handles it:

import csv, glob

def merge_csvs(pattern, output):
    all_rows = []
    all_fields = set()
    for f in glob.glob(pattern):
        with open(f) as fh:
            reader = csv.DictReader(fh)
            all_fields.update(reader.fieldnames)
            all_rows.extend(list(reader))
    with open(output, 'w', newline='') as fh:
        writer = csv.DictWriter(fh, fieldnames=sorted(all_fields))
        writer.writeheader()
        writer.writerows(all_rows)
Enter fullscreen mode Exit fullscreen mode

3. Duplicate File Finder

Found 40GB of duplicate backups on a client's server with this:

import hashlib, os
from collections import defaultdict

def find_dupes(directory):
    hashes = defaultdict(list)
    for root, _, files in os.walk(directory):
        for f in files:
            path = os.path.join(root, f)
            h = hashlib.md5(open(path, 'rb').read()).hexdigest()
            hashes[h].append(path)
    return {h: paths for h, paths in hashes.items() if len(paths) > 1}
Enter fullscreen mode Exit fullscreen mode

4. API Response Cacher

Wrap any API call with a TTL cache decorator:

import json, time, os, hashlib

def cached(ttl=3600, cache_dir='.cache'):
    os.makedirs(cache_dir, exist_ok=True)
    def decorator(func):
        def wrapper(*args, **kwargs):
            key = hashlib.md5(str((args, kwargs)).encode()).hexdigest()
            path = os.path.join(cache_dir, f"{key}.json")
            if os.path.exists(path):
                data = json.load(open(path))
                if time.time() - data['ts'] < ttl:
                    return data['result']
            result = func(*args, **kwargs)
            json.dump({'ts': time.time(), 'result': result}, open(path, 'w'))
            return result
        return wrapper
    return decorator
Enter fullscreen mode Exit fullscreen mode

5. Directory Watcher

Monitor a folder and trigger actions on new files:

import time, os

def watch(directory, callback, interval=2):
    seen = set(os.listdir(directory))
    while True:
        current = set(os.listdir(directory))
        new = current - seen
        for f in new:
            callback(os.path.join(directory, f))
        seen = current
        time.sleep(interval)
Enter fullscreen mode Exit fullscreen mode

Get the Full Toolkit

These are 5 of the 10 scripts in my Python Automation Toolkit — the full package includes PDF extraction, JSON/CSV conversion, email sender, web scraper template, and a pure-Python scheduler.

Each script is standalone, commented, and production-tested. $12 for the lot.

Also available: 50 Regex Patterns Cookbook for text processing.

Top comments (0)