Automation & scripting
CLI tools, scheduled jobs, filesystem tasks, and glue scripts that save time.
Extract Attachments from mbox Mailbox Files in Python
Extract file attachments from an mbox mailbox format using Python's standard library email and mailbox modules.
import email
import mailbox
from email.policy import default
from pathlib import Path
def extract_attachments(mbox_path, output_dir):
output_dir = Path(output_dir)
output_dir.mkdir(exist_ok=True)
mbox = mailbox.mbox(mbox_path)
for msg in mbox:
if msg.is_multipart():
for part i…
Scrape HTML Tables in Python with html.parser
Extract data from HTML tables using Python's built-in html.parser module, without third-party dependencies, by overriding callback methods to track table, row, and cell states.
import html.parser
from urllib.request import urlopen
class TableParser(html.parser.HTMLParser):
def __init__(self):
super().__init__()
self.in_table = False
self.in_row = False
self.in_cell = False
self.current_cell = []
self.rows = []
self.row = []
d…
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.