_sanitize_filename

AIFT · files, filename-sanitization, filesystem-safety, input-sanitization

Replaces non-alphanumeric characters with underscores to create filesystem-safe filenames, defaulting to 'artifact' if empty.

    def _sanitize_filename(value: str) -> str:
        """Replace non-alphanumeric characters with underscores for safe filenames.

        Args:
            value: Raw string to sanitise.

        Returns:
            Filesystem-safe string, or ``"artifact"`` if empty after cleaning.
        """
        cleaned = re.sub(r"[^A-Za-z0-9._-]+", "_", value).strip("_")
        return cleaned or "artifact"

See the full source, get the GitHub permalink, and search 40K more like it.

Get a free API key