Automation & scripting
CLI tools, scheduled jobs, filesystem tasks, and glue scripts that save time.
How to rename music files by ID3 tags in Python
Renames MP3 files in a folder using artist and title extracted from ID3 tags, with a mock fallback that parses filenames.
import os
import re
from pathlib import Path
def sanitize_filename(name: str) -> str:
return re.sub(r'[<>:"/\\|?*]', '_', name).strip()
def rename_mp3_from_id3(path: Path) -> None:
for f in path.glob("*.mp3"):
# Mock ID3 extraction: derive artist/title from filename
stem = f.stem
if "…
Rename Files in Folder with Numeric Prefix in Python
Renames all files in a folder by adding a sequential numeric prefix (e.g., 01_, 02_) to each filename using pathlib.
from pathlib import Path
def rename_with_numeric_prefix(folder_path):
folder = Path(folder_path)
for index, file_path in enumerate(folder.iterdir(), start=1):
if file_path.is_file():
new_name = f"{index:02d}_{file_path.name}"
new_path = file_path.with_name(new_name)
…
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.