Claude Code for fnmatch: Python Unix Shell Pattern Matching — Claude Skills 360 Blog
Blog / AI / Claude Code for fnmatch: Python Unix Shell Pattern Matching
AI

Claude Code for fnmatch: Python Unix Shell Pattern Matching

Published: August 23, 2028
Read time: 5 min read
By: Claude Skills 360

Python’s fnmatch module matches filenames against Unix shell-style wildcard patterns. import fnmatch. fnmatch: fnmatch.fnmatch(name, pattern) → bool; case-insensitive on Windows, case-sensitive on Unix; patterns: * = any number of chars, ? = exactly one char, [seq] = char in seq, [!seq] = char not in seq. fnmatchcase: fnmatch.fnmatchcase(name, pattern) → always case-sensitive. filter: fnmatch.filter(names, pattern) → list of matching names from names iterable; equivalent to [n for n in names if fnmatch(n, pattern)]. translate: fnmatch.translate(pattern) → regex string corresponding to the shell pattern; compile with re.compile(fnmatch.translate(p)) for repeated use. Pattern examples: "*.py" matches Python files; "test_*.py" matches test modules; "[!._]*" excludes hidden/private files; "v[0-9].*" matches version files; "????.*" matches four-char basenames. Unlike glob, fnmatch operates on strings only — it does not touch the filesystem. Claude Code generates file filter pipelines, log file selectors, asset inclusion rules, and pattern-based routing tables.

CLAUDE.md for fnmatch

## fnmatch Stack
- Stdlib: import fnmatch
- Match:   fnmatch.fnmatch("app.py", "*.py")      # True
- Case:    fnmatch.fnmatchcase("App.PY", "*.py")  # False
- Filter:  fnmatch.filter(names, "test_*.py")
- Regex:   re.compile(fnmatch.translate("*.py"))   # for repeated use
- Multi:   any(fnmatch.fnmatch(n, p) for p in patterns)

fnmatch File Pattern Pipeline

# app/fnutil.py — pattern matching, filter, multi-pattern, rule engine
from __future__ import annotations

import fnmatch
import re
from dataclasses import dataclass
from pathlib import Path
from typing import Callable, Iterable


# ─────────────────────────────────────────────────────────────────────────────
# 1. Pattern matching helpers
# ─────────────────────────────────────────────────────────────────────────────

def matches(name: str, pattern: str, case_sensitive: bool = True) -> bool:
    """
    Return True if name matches the shell-style pattern.

    Example:
        matches("report_2025.csv", "report_*.csv")  # True
        matches("README.MD", "*.md", case_sensitive=False)  # True
    """
    if case_sensitive:
        return fnmatch.fnmatchcase(name, pattern)
    return fnmatch.fnmatch(name, pattern)


def matches_any(name: str, patterns: Iterable[str], case_sensitive: bool = True) -> bool:
    """
    Return True if name matches any of the given patterns.

    Example:
        matches_any("app.py", ["*.py", "*.pyi"])  # True
        matches_any("app.js", ["*.py", "*.pyi"])  # False
    """
    fn = fnmatch.fnmatchcase if case_sensitive else fnmatch.fnmatch
    return any(fn(name, p) for p in patterns)


def matches_all(name: str, patterns: Iterable[str], case_sensitive: bool = True) -> bool:
    """
    Return True only if name matches every pattern in the list.

    Example:
        matches_all("test_app.py", ["test_*", "*.py"])  # True
        matches_all("app.py", ["test_*", "*.py"])       # False
    """
    fn = fnmatch.fnmatchcase if case_sensitive else fnmatch.fnmatch
    return all(fn(name, p) for p in patterns)


def filter_names(names: Iterable[str], pattern: str, case_sensitive: bool = True) -> list[str]:
    """
    Return names that match pattern.

    Example:
        filter_names(["app.py", "app.js", "test.py"], "*.py")
        # ['app.py', 'test.py']
    """
    if case_sensitive:
        return [n for n in names if fnmatch.fnmatchcase(n, pattern)]
    return fnmatch.filter(list(names), pattern)


def filter_multi(names: Iterable[str], patterns: Iterable[str], case_sensitive: bool = True) -> list[str]:
    """
    Return names that match at least one of the patterns.

    Example:
        filter_multi(files, ["*.py", "*.pyi", "*.pyx"])
    """
    pats = list(patterns)
    return [n for n in names if matches_any(n, pats, case_sensitive=case_sensitive)]


def exclude_patterns(names: Iterable[str], patterns: Iterable[str], case_sensitive: bool = True) -> list[str]:
    """
    Return names that do NOT match any of the exclude patterns.

    Example:
        exclude_patterns(files, ["*.pyc", "__pycache__", ".DS_Store"])
    """
    pats = list(patterns)
    return [n for n in names if not matches_any(n, pats, case_sensitive=case_sensitive)]


# ─────────────────────────────────────────────────────────────────────────────
# 2. Pattern compilation for repeated use
# ─────────────────────────────────────────────────────────────────────────────

class CompiledPattern:
    """
    Pre-compiled fnmatch pattern for efficient repeated matching.

    Example:
        p = CompiledPattern("*.py")
        p.matches("app.py")          # True
        p.filter(["a.py", "b.js"])   # ['a.py']
    """

    def __init__(self, pattern: str, case_sensitive: bool = True) -> None:
        self.pattern = pattern
        self.case_sensitive = case_sensitive
        flags = 0 if case_sensitive else re.IGNORECASE
        self._regex = re.compile(fnmatch.translate(pattern), flags)

    def matches(self, name: str) -> bool:
        return bool(self._regex.match(name))

    def filter(self, names: Iterable[str]) -> list[str]:
        return [n for n in names if self._regex.match(n)]

    def __repr__(self) -> str:
        return f"CompiledPattern({self.pattern!r}, case_sensitive={self.case_sensitive})"


class PatternSet:
    """
    A set of include and exclude patterns for file selection.

    Example:
        ps = PatternSet(include=["*.py", "*.pyi"], exclude=["test_*", "*_test.py"])
        ps.matches("app.py")        # True
        ps.matches("test_app.py")   # False
        ps.filter(file_list)
    """

    def __init__(
        self,
        include: list[str] | None = None,
        exclude: list[str] | None = None,
        case_sensitive: bool = True,
    ) -> None:
        self._include = [CompiledPattern(p, case_sensitive) for p in (include or [])]
        self._exclude = [CompiledPattern(p, case_sensitive) for p in (exclude or [])]
        self._has_include = bool(self._include)

    def matches(self, name: str) -> bool:
        if any(ep.matches(name) for ep in self._exclude):
            return False
        if self._has_include:
            return any(ip.matches(name) for ip in self._include)
        return True

    def filter(self, names: Iterable[str]) -> list[str]:
        return [n for n in names if self.matches(n)]


# ─────────────────────────────────────────────────────────────────────────────
# 3. Filesystem helpers
# ─────────────────────────────────────────────────────────────────────────────

def list_matching(directory: str | Path, pattern: str, recursive: bool = False) -> list[Path]:
    """
    List files in directory matching pattern (by basename).

    Example:
        list_matching("src", "*.py")
        list_matching(".", "*.log", recursive=True)
    """
    base = Path(directory)
    if recursive:
        return [p for p in base.rglob("*") if p.is_file() and fnmatch.fnmatchcase(p.name, pattern)]
    return [p for p in base.iterdir() if p.is_file() and fnmatch.fnmatchcase(p.name, pattern)]


def list_matching_multi(
    directory: str | Path,
    patterns: list[str],
    exclude: list[str] | None = None,
    recursive: bool = False,
) -> list[Path]:
    """
    List files matching any include pattern and not matching any exclude pattern.

    Example:
        list_matching_multi("src", ["*.py", "*.pyi"], exclude=["*_pb2.py"])
    """
    base = Path(directory)
    walk = base.rglob("*") if recursive else base.iterdir()
    ps = PatternSet(include=patterns, exclude=exclude or [])
    return [p for p in walk if p.is_file() and ps.matches(p.name)]


def find_by_stem(directory: str | Path, stem_pattern: str) -> list[Path]:
    """
    Find files whose stem (name without extension) matches the given pattern.

    Example:
        find_by_stem("logs", "app_2025-??-??")  # e.g. app_2025-03-15.log
    """
    base = Path(directory)
    return [
        p for p in base.iterdir()
        if p.is_file() and fnmatch.fnmatchcase(p.stem, stem_pattern)
    ]


# ─────────────────────────────────────────────────────────────────────────────
# 4. Pattern routing and dispatch
# ─────────────────────────────────────────────────────────────────────────────

@dataclass
class Route:
    pattern:  str
    handler:  Callable[[str], Any]
    priority: int = 0

    def matches(self, name: str) -> bool:
        return fnmatch.fnmatchcase(name, self.pattern)


class PatternRouter:
    """
    Route file names (or any strings) to handlers based on fnmatch patterns.
    First matching route wins (by priority desc, then registration order).

    Example:
        router = PatternRouter()
        router.add("*.log",    handle_log,    priority=10)
        router.add("*.csv",    handle_csv)
        router.add("error_*",  handle_error,  priority=20)
        router.route("error_app.log")   # → handle_error
    """

    def __init__(self) -> None:
        self._routes: list[Route] = []

    def add(self, pattern: str, handler: Callable[[str], Any], priority: int = 0) -> None:
        self._routes.append(Route(pattern=pattern, handler=handler, priority=priority))
        self._routes.sort(key=lambda r: -r.priority)

    def route(self, name: str) -> Any:
        for r in self._routes:
            if r.matches(name):
                return r.handler(name)
        raise KeyError(f"No route matched: {name!r}")

    def find_route(self, name: str) -> Route | None:
        return next((r for r in self._routes if r.matches(name)), None)


# ─────────────────────────────────────────────────────────────────────────────
# 5. Pattern utilities
# ─────────────────────────────────────────────────────────────────────────────

def pattern_to_regex(pattern: str, flags: int = 0) -> re.Pattern[str]:
    """
    Convert a shell pattern to a compiled regex.

    Example:
        rx = pattern_to_regex("*.py")
        rx.match("app.py")  # Match object
    """
    return re.compile(fnmatch.translate(pattern), flags)


def categorize(names: Iterable[str], categories: dict[str, list[str]]) -> dict[str, list[str]]:
    """
    Assign each name to all matching categories.
    categories: {category_name: [patterns]}

    Example:
        cats = categorize(
            ["app.py", "style.css", "index.html", "data.json"],
            {"python": ["*.py", "*.pyi"], "web": ["*.html", "*.css", "*.js"]},
        )
        # {'python': ['app.py'], 'web': ['style.css', 'index.html']}
    """
    result: dict[str, list[str]] = {cat: [] for cat in categories}
    for name in names:
        for cat, pats in categories.items():
            if matches_any(name, pats):
                result[cat].append(name)
    return result


# ─────────────────────────────────────────────────────────────────────────────
# Demo
# ─────────────────────────────────────────────────────────────────────────────

if __name__ == "__main__":
    from typing import Any

    print("=== fnmatch demo ===")

    files = [
        "app.py", "test_app.py", "app_test.py", "app.pyi", "app.js",
        "README.md", "requirements.txt", "setup.py", "conftest.py",
        "data.csv", "report_2025.csv", ".gitignore", "__pycache__",
    ]

    print("\n--- matches / matches_any ---")
    print(f"  matches('app.py', '*.py')                : {matches('app.py', '*.py')}")
    print(f"  matches_any('app.pyi', ['*.py','*.pyi']) : {matches_any('app.pyi', ['*.py','*.pyi'])}")
    print(f"  matches_all('test_app.py', ['test_*','*.py']): {matches_all('test_app.py', ['test_*','*.py'])}")

    print("\n--- filter_names / filter_multi ---")
    py_files = filter_names(files, "*.py")
    print(f"  *.py files:        {py_files}")
    src_files = filter_multi(files, ["*.py", "*.pyi", "*.js"])
    print(f"  src files:         {src_files}")
    clean = exclude_patterns(files, ["__pycache__", ".gitignore", "*.pyc"])
    print(f"  after exclusions:  {clean}")

    print("\n--- CompiledPattern ---")
    p = CompiledPattern("test_*.py")
    print(f"  test_*.py matches: {p.filter(files)}")

    print("\n--- PatternSet ---")
    ps = PatternSet(include=["*.py", "*.pyi"], exclude=["test_*", "*_test.py", "conftest.py"])
    print(f"  include py, exclude tests: {ps.filter(files)}")

    print("\n--- categorize ---")
    cats = categorize(files, {
        "python": ["*.py", "*.pyi"],
        "data":   ["*.csv", "*.json"],
        "config": ["*.txt", "*.md", ".gitignore"],
    })
    for cat, matched in cats.items():
        print(f"  {cat}: {matched}")

    print("\n--- PatternRouter ---")
    router = PatternRouter()
    router.add("test_*.py", lambda n: f"TEST:{n}",   priority=20)
    router.add("*.py",      lambda n: f"SOURCE:{n}",  priority=10)
    router.add("*.csv",     lambda n: f"DATA:{n}")
    for fname in ["app.py", "test_app.py", "data.csv"]:
        print(f"  route({fname!r}) → {router.route(fname)}")

    print("\n--- pattern_to_regex ---")
    rx = pattern_to_regex("report_????-??-??.csv")
    test_names = ["report_2025-01-15.csv", "report_2025.csv", "report_2025-01-15.log"]
    for n in test_names:
        print(f"  {n!r:40s}: {bool(rx.match(n))}")

    print("\n=== done ===")

For the glob alternative — glob.glob() and Path.glob() apply shell patterns directly to the filesystem and return matching file paths; fnmatch operates on plain strings without touching the filesystem — use glob when you need to discover files on disk; use fnmatch when you already have a list of names (from an archive, a database, an in-memory index, or a remote API listing) and need to filter it without performing filesystem I/O. For the re alternative — re provides full regular-expression power with groups, lookaheads, and anchors; fnmatch.translate() converts a shell pattern to a regex string, so the two are interoperable — use re directly when your matching rules are complex enough to need regex syntax; use fnmatch for the familiar *, ?, [seq] shell-pattern vocabulary that users of .gitignore files or shell globbing already know, since it is easier to read and configure without regex expertise. The Claude Skills 360 bundle includes fnmatch skill sets covering matches()/matches_any()/matches_all()/filter_names()/filter_multi()/exclude_patterns() core helpers, CompiledPattern and PatternSet classes with include/exclude support, list_matching()/list_matching_multi()/find_by_stem() filesystem utilities, PatternRouter for pattern-based dispatch, and categorize() for multi-category classification. Start with the free tier to try filename pattern matching and fnmatch pipeline code generation.

Keep Reading

AI

Claude Code for email.contentmanager: Python Email Content Accessors

Read and write EmailMessage body content with Python's email.contentmanager module and Claude Code — email contentmanager ContentManager for the class that maps content types to get and set handler functions allowing EmailMessage to support get_content and set_content with type-specific behaviour, email contentmanager raw_data_manager for the ContentManager instance that handles raw bytes and str payloads without any conversion, email contentmanager content_manager for the standard ContentManager instance used by email.policy.default that intelligently handles text plain text html multipart and binary content types, email contentmanager get_content_text for the handler that returns the decoded text payload of a text-star message part as a str, email contentmanager get_content_binary for the handler that returns the raw decoded bytes payload of a non-text message part, email contentmanager get_data_manager for the get-handler lookup used by EmailMessage get_content to find the right reader function for the content type, email contentmanager set_content text for the handler that creates and sets a text part correctly choosing charset and transfer encoding, email contentmanager set_content bytes for the handler that creates and sets a binary part with base64 encoding and optional filename Content-Disposition, email contentmanager EmailMessage get_content for the method that reads the message body using the registered content manager handlers, email contentmanager EmailMessage set_content for the method that sets the message body and MIME headers in one call, email contentmanager EmailMessage make_alternative make_mixed make_related for the methods that convert a simple message into a multipart container, email contentmanager EmailMessage add_attachment for the method that attaches a file or bytes to a multipart message, and email contentmanager integration with email.message and email.policy and email.mime and io for building high-level email readers attachment extractors text body accessors HTML readers and policy-aware MIME construction pipelines.

5 min read Feb 12, 2029
AI

Claude Code for email.charset: Python Email Charset Encoding

Control header and body encoding for international email with Python's email.charset module and Claude Code — email charset Charset for the class that wraps a character set name with the encoding rules for header encoding and body encoding describing how to encode text for that charset in email messages, email charset Charset header_encoding for the attribute specifying whether headers using this charset should use QP quoted-printable encoding BASE64 encoding or no encoding, email charset Charset body_encoding for the attribute specifying the Content-Transfer-Encoding to use for message bodies in this charset such as QP or BASE64, email charset Charset output_codec for the attribute giving the Python codec name used to encode the string to bytes for the wire format, email charset Charset input_codec for the attribute giving the Python codec name used to decode incoming bytes to str, email charset Charset get_output_charset for returning the output charset name, email charset Charset header_encode for encoding a header string using the charset's header_encoding method, email charset Charset body_encode for encoding body content using the charset's body_encoding, email charset Charset convert for converting a string from the input_codec to the output_codec, email charset add_charset for registering a new charset with custom encoding rules in the global charset registry, email charset add_alias for adding an alias name that maps to an existing registered charset, email charset add_codec for registering a codec name mapping for use by the charset machinery, and email charset integration with email.message and email.mime and email.policy and email.encoders for building international email senders non-ASCII header encoders Content-Transfer-Encoding selectors charset-aware message constructors and MIME encoding pipelines.

5 min read Feb 11, 2029
AI

Claude Code for email.utils: Python Email Address and Header Utilities

Parse and format RFC 2822 email addresses and dates with Python's email.utils module and Claude Code — email utils parseaddr for splitting a display-name plus angle-bracket address string into a realname and email address tuple, email utils formataddr for combining a realname and address string into a properly quoted RFC 2822 address with angle brackets, email utils getaddresses for parsing a list of raw address header strings each potentially containing multiple comma-separated addresses into a list of realname address tuples, email utils parsedate for parsing an RFC 2822 date string into a nine-tuple compatible with time.mktime, email utils parsedate_tz for parsing an RFC 2822 date string into a ten-tuple that includes the UTC offset timezone in seconds, email utils parsedate_to_datetime for parsing an RFC 2822 date string into an aware datetime object with timezone, email utils formatdate for formatting a POSIX timestamp or the current time as an RFC 2822 date string with optional usegmt and localtime flags, email utils format_datetime for formatting a datetime object as an RFC 2822 date string, email utils make_msgid for generating a globally unique Message-ID string with optional idstring and domain components, email utils decode_rfc2231 for decoding an RFC 2231 encoded parameter value into a tuple of charset language and value, email utils encode_rfc2231 for encoding a string as an RFC 2231 encoded parameter value, email utils collapse_rfc2231_value for collapsing a decoded RFC 2231 tuple to a Unicode string, and email utils integration with email.message and email.headerregistry and datetime and time for building address parsers date formatters message-id generators header extractors and RFC-compliant email construction utilities.

5 min read Feb 10, 2029

Put these ideas into practice

Claude Skills 360 gives you production-ready skills for everything in this article — and 2,350+ more. Start free or go all-in.

Back to Blog

Get 360 skills free