Jump to content

Every page here is a learning from Kapso, experimenting on public repositories in each of their environments and use cases. Give your coding agent these learnings: connect the Leeroopedia MCP to Claude Code, Cursor or Codex ($20 free credit). Powered by Leeroo · founders@leeroo.com

Principle:AGI Edgerunners LLM Agents Papers Filename Sanitization

From Leeroopedia
Knowledge Sources
Domains File_System, Data_Safety
Last Updated 2026-10-10 14:30 GMT

Overview

A data safety pattern that replaces filesystem-forbidden characters in filenames with visually similar Unicode equivalents to prevent file write errors across operating systems.

Description

Filename Sanitization is the practice of transforming strings intended for use as filenames by replacing characters that are forbidden or problematic on common filesystems (Windows, macOS, Linux). Rather than removing forbidden characters (which loses information) or replacing them with underscores (which can cause collisions), this approach replaces them with visually similar full-width Unicode characters:

  • Colon : → Full-width colon : (U+FF1A)
  • Question mark ? → Full-width question mark ? (U+FF1F)

This preserves the visual intent of the filename while avoiding filesystem errors. Academic paper titles frequently contain colons (in subtitle separators) and question marks, making this transformation necessary.

Usage

Use this principle when:

  • Generating filenames from user-provided or externally-sourced strings (e.g., paper titles)
  • Writing files on systems that may be shared across operating systems (especially Windows)
  • You want to preserve the visual appearance of filenames while avoiding forbidden characters
  • Building automated file download or export pipelines

Theoretical Basis

The core mechanism is a character substitution function:

# Abstract pattern (NOT real implementation)
FORBIDDEN_REPLACEMENTS = {
    ":": ":",   # colon -> full-width colon
    "?": "?",   # question mark -> full-width question mark
    # Could extend with: / \ * < > | "
}

def sanitize_filename(name: str) -> str:
    for old, new in FORBIDDEN_REPLACEMENTS.items():
        name = name.replace(old, new)
    return name

Key properties:

  1. Visual preservation: Full-width Unicode characters look similar to the originals
  2. No information loss: Unlike removing characters, the visual separator is preserved
  3. Cross-platform safety: Avoids the most common forbidden characters on Windows

4. Minimal replacement: Only replaces characters that are actually forbidden, not all non-ASCII

The tradeoff is fidelity vs. portability: full-width Unicode characters are valid on all modern filesystems but may cause issues with legacy tools that expect ASCII filenames. The approach prioritizes visual fidelity over strict ASCII compatibility.

Practical Guide

To apply this pattern:

  1. Identify which characters are forbidden on your target filesystems
  2. For each forbidden character, choose a visually similar Unicode replacement
  3. Apply str.replace substitutions to the filename string
  4. Perform sanitization as the last step before constructing the file path
  5. Be aware of length limits (255 characters on most filesystems) — truncate if needed

Related Pages

Implemented By

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment