Over the years, I've built a collection of Python scripts that I reuse on almost every project. These aren't fancy frameworks — they're small, focused utilities that save me hours every week.
Here are the 10 scripts I reach for most often. All are production-tested and dead simple to integrate.
1. Bulk File Renamer
Need to rename hundreds of files with a pattern? This handles it in seconds:
import os
import re
from pathlib import Path
def bulk_rename(directory, pattern, replacement, dry_run=True):
"""Rename files matching a regex pattern."""
renamed = []
for filepath in Path(directory).iterdir():
if filepath.is_file():
new_name = re.sub(pattern, replacement, filepath.name)
if new_name != filepath.name:
new_path = filepath.parent / new_name
if dry_run:
print(f"[DRY RUN] {filepath.name} → {new_name}")
else:
filepath.rename(new_path)
print(f"Renamed: {filepath.name} → {new_name}")
renamed.append((filepath.name, new_name))
return renamed
# Example: Remove spaces from all .jpg files
bulk_rename("./photos", r"\s+", "_", dry_run=False)
2. CSV to JSON Converter
APIs love JSON. Your clients send CSV. Bridge the gap:
import csv
import json
def csv_to_json(csv_path, json_path, encoding='utf-8'):
"""Convert CSV file to JSON array."""
with open(csv_path, encoding=encoding) as f:
reader = csv.DictReader(f)
data = list(reader)
with open(json_path, 'w') as f:
json.dump(data, f, indent=2)
print(f"Converted {len(data)} rows → {json_path}")
return data
csv_to_json("data.csv", "data.json")
3. Website Health Checker
Monitor your endpoints without paying for a service:
import requests
import time
from datetime import datetime
def check_endpoints(urls, timeout=10):
"""Check a list of URLs and report status."""
results = []
for url in urls:
try:
start = time.time()
resp = requests.get(url, timeout=timeout)
elapsed = time.time() - start
results.append({
"url": url,
"status": resp.status_code,
"response_time": round(elapsed, 2),
"healthy": resp.status_code < 400,
"checked_at": datetime.now().isoformat(),
})
except requests.RequestException as e:
results.append({
"url": url,
"status": None,
"healthy": False,
"error": str(e),
})
for r in results:
icon = "✅" if r["healthy"] else "❌"
print(f"{icon} {r['url']} — {r.get('status', 'FAIL')} ({r.get('response_time', '-')}s)")
return results
check_endpoints([
"https://mysite.com",
"https://mysite.com/api/health",
])
4. Duplicate File Finder
Your Downloads folder is 47GB. Here's why:
import hashlib
from pathlib import Path
from collections import defaultdict
def find_duplicates(directory):
"""Find duplicate files by content hash."""
hashes = defaultdict(list)
for filepath in Path(directory).rglob("*"):
if filepath.is_file():
h = hashlib.md5(filepath.read_bytes()).hexdigest()
hashes[h].append(filepath)
duplicates = {h: paths for h, paths in hashes.items() if len(paths) > 1}
total_wasted = 0
for h, paths in duplicates.items():
size = paths[0].stat().st_size
wasted = size * (len(paths) - 1)
total_wasted += wasted
print(f"\n🔍 {len(paths)} copies ({size:,} bytes each):")
for p in paths:
print(f" {p}")
print(f"\n💾 Total wasted space: {total_wasted / 1024 / 1024:.1f} MB")
return duplicates
find_duplicates("/home/user/Downloads")
5. Quick Screenshot API
Turn any webpage into an image — great for monitoring or thumbnails:
from selenium import webdriver
from selenium.webdriver.chrome.options import Options
def take_screenshot(url, output_path, width=1280, height=720):
"""Capture a screenshot of a webpage."""
options = Options()
options.add_argument("--headless")
options.add_argument("--no-sandbox")
options.add_argument(f"--window-size={width},{height}")
driver = webdriver.Chrome(options=options)
try:
driver.get(url)
driver.save_screenshot(output_path)
print(f"Screenshot saved: {output_path}")
finally:
driver.quit()
take_screenshot("https://example.com", "screenshot.png")
6. Environment Variable Validator
Catch missing env vars before your app crashes at 3 AM:
import os
import sys
def validate_env(required_vars, optional_vars=None):
"""Validate that all required environment variables are set."""
missing = [var for var in required_vars if not os.getenv(var)]
if missing:
print("❌ Missing required environment variables:")
for var in missing:
print(f" - {var}")
sys.exit(1)
if optional_vars:
for var in optional_vars:
if not os.getenv(var):
print(f"⚠️ Optional variable not set: {var}")
print(f"✅ All {len(required_vars)} required variables present")
return True
validate_env(
required_vars=["DATABASE_URL", "SECRET_KEY", "API_KEY"],
optional_vars=["REDIS_URL", "SENTRY_DSN"],
)
7. Log File Analyzer
Find the needle in your haystack of logs:
import re
from collections import Counter
def analyze_logs(log_path, pattern=None):
"""Analyze log files with optional filtering."""
errors = Counter()
total_lines = 0
with open(log_path) as f:
for line in f:
total_lines += 1
if pattern and not re.search(pattern, line, re.IGNORECASE):
continue
if "ERROR" in line or "CRITICAL" in line:
match = re.search(r'(ERROR|CRITICAL)\s+(\w+)', line)
if match:
errors[match.group(2)] += 1
print(f"Analyzed {total_lines:,} lines")
print(f"\nTop errors:")
for error_type, count in errors.most_common(10):
print(f" {count:>5}x {error_type}")
return errors
analyze_logs("/var/log/app.log", pattern=r"api|endpoint")
8. Batch Image Resizer
Resize thousands of images for your app or website:
from PIL import Image
from pathlib import Path
def batch_resize(input_dir, output_dir, max_size=(800, 800), quality=85):
"""Resize all images in a directory."""
output = Path(output_dir)
output.mkdir(exist_ok=True)
processed = 0
for img_path in Path(input_dir).glob("*"):
if img_path.suffix.lower() in ('.jpg', '.jpeg', '.png', '.webp'):
with Image.open(img_path) as img:
img.thumbnail(max_size, Image.Resampling.LANCZOS)
save_path = output / img_path.name
img.save(save_path, quality=quality)
processed += 1
print(f"Resized {processed} images → {output_dir}")
batch_resize("./raw_photos", "./web_photos", max_size=(1200, 1200))
9. API Rate Limiter Decorator
Protect yourself from rate limits without thinking about it:
import time
import functools
def rate_limit(calls_per_second=1):
"""Decorator to rate-limit function calls."""
min_interval = 1.0 / calls_per_second
last_called = [0.0]
def decorator(func):
@functools.wraps(func)
def wrapper(*args, **kwargs):
elapsed = time.time() - last_called[0]
if elapsed < min_interval:
time.sleep(min_interval - elapsed)
last_called[0] = time.time()
return func(*args, **kwargs)
return wrapper
return decorator
@rate_limit(calls_per_second=2)
def call_api(url):
"""Call an API at max 2 requests/second."""
import requests
return requests.get(url)
10. Project Scaffolder
Generate boilerplate for new projects in one command:
from pathlib import Path
def scaffold_project(name, project_type="python"):
"""Generate a new project structure."""
templates = {
"python": [
"src/{name}/__init__.py",
"src/{name}/main.py",
"tests/__init__.py",
"tests/test_main.py",
"requirements.txt",
"README.md",
".gitignore",
"Makefile",
],
"api": [
"app/__init__.py",
"app/main.py",
"app/routes/__init__.py",
"app/models/__init__.py",
"app/config.py",
"tests/__init__.py",
"requirements.txt",
"Dockerfile",
"docker-compose.yml",
"README.md",
],
}
for template in templates.get(project_type, templates["python"]):
filepath = Path(name) / template.format(name=name)
filepath.parent.mkdir(parents=True, exist_ok=True)
filepath.touch()
print(f"Created: {filepath}")
print(f"\n✅ Project '{name}' scaffolded with {project_type} template")
scaffold_project("my-new-app", "api")
Get the Complete Collection
These 10 scripts are just the beginning. I've compiled 50+ production-ready Python scripts into a single collection — complete with documentation, tests, and real-world examples.
Get the Python Revenue Engine for $19 →
The full collection includes scripts for:
- Automation: Email senders, web scrapers, file organizers
- Data Processing: ETL pipelines, report generators, data cleaners
- DevOps: Deployment scripts, monitoring tools, log analyzers
- Monetization: SaaS boilerplates, payment integrations, analytics dashboards
- AI/ML: LLM wrappers, embedding generators, prompt optimizers
Each script is standalone — no complex dependencies, no framework lock-in. Just copy, paste, and customize.
Stop Rewriting the Same Code
The biggest productivity killer isn't bad tools — it's rewriting utilities you've built before. Keep a toolkit. Build it up over time. And when someone else has already done the hard work, use their collection.
Your time is worth more than $19.
What Python scripts do you reach for constantly? Share your favorites in the comments.
Top comments (0)