Automation & scripting
CLI tools, scheduled jobs, filesystem tasks, and glue scripts that save time.
Benchmark Disk Write Speed in Python with tempfile
Benchmark raw disk write performance by writing a temporary file in 1MB chunks and measuring throughput in MB/s.
import os
import tempfile
import time
def benchmark_write(size_mb=50):
size_bytes = size_mb * 1024 * 1024
chunk = b'x' * 1024 * 1024 # 1 MB chunk
with tempfile.NamedTemporaryFile(delete=True) as tmp:
start = time.perf_counter()
written = 0
while written < size_bytes:
…
How to Sync Two Directories in Python (rsync-like)
Mirror a source directory into a destination by copying new or changed files and deleting extras, similar to rsync.
import os
import shutil
import sys
from pathlib import Path
def sync_dirs(src: Path, dst: Path):
"""Mirror src into dst: copy new files, overwrite changed, delete extras."""
dst.mkdir(parents=True, exist_ok=True)
for dst_entry in dst.rglob('*'):
rel = dst_entry.relative_to(dst)
src_entry =…
Browse by section
Each section groups closely related Python snippets.
Automation & scripting — Python code examples
What you will find here
This page collects automation & scripting snippets — short, copy-ready Python you can paste into our free online IDE and run without installing anything. Each sample includes a plain-English explanation and the full source code.
Samples vs tutorials and challenges
Samples are quick reference — one concept per page. For step-by-step teaching, use our Python tutorials. To test yourself, try quizzes or coding challenges. Clean up style with the Python formatter.