Path.with_suffix

Path.with_suffix(...)

Description

Documentation for Path.with_suffix.

Python Python Pathlib Official Docs

Real-World Examples

Practical code examples showing how Path.with_suffix is used in real projects.

def load_configs_from_disk(input_dir: Path) -> Iterator[ConfigFile]:
    """Load configs from disk cache."""
    for filepath in input_dir.glob("*.lua"):
        if filepath.name.endswith(".meta"):
            continue
        meta_path = filepath.with_suffix(".lua.meta")
        content = filepath.read_text(encoding="utf-8")

        # Parse metadata if exists
        meta = {}
        if meta_path.exists():
            for line in meta_path.read_text().splitlines():
                if "=" in line:
                    k, v = line.split("=", 1)
                    meta[k] = v

        parts = filepath.stem.split("__", 1)
        owner = parts[0] if len(parts) > 1 else "unknown"
        name = parts[1] if len(parts) > 1 else filepath.stem

        yield ConfigFile(
            repo=RepoInfo(
                owner=owner,
                name=name,
                url=meta.get("url", ""),
def save_history(data):

    """Save history.json atomically"""

    history_path = get_history_path()

    temp_file = history_path.with_suffix(".json.tmp")

    with open(temp_file, "w", encoding="utf-8") as f:

        json.dump(data, f, indent=2, ensure_ascii=False)

    temp_file.replace(history_path)


def get_url_hash(url):

    """Generate SHA256 hash of normalized target URL for dedup"""

    # Normalize: strip query params, trailing slashes, lowercase

    parsed = urllib.parse.urlparse(url)

    clean = f"{parsed.scheme}://{parsed.netloc}{parsed.path.rstrip('/')}".lower()

    return hashlib.sha256(clean.encode("utf-8")).hexdigest()


def cmd_log(args):

    """Log a posted reply to history"""

    history = load_history()

    url_hash = get_url_hash(args.target_url)

    today = datetime.now().strftime("%Y-%m-%d")