Created
July 6, 2025 01:40
-
-
Save yeiichi/d3d2f974eff0dd0855ce784cf79d9f33 to your computer and use it in GitHub Desktop.
Sanitize a string to make it safe for use as a filename
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import re | |
# macOS/Windows compatibility | |
INVALID_FILENAME_CHARS = re.compile(r'[<>:"/\\|?*\x00-\x1F]|[\s.]$') | |
def sanitize_filename(filename: str) -> str: | |
""" | |
Sanitizes a string to make it safe for use as a filename. | |
Replaces problematic characters with a hyphen (-). | |
""" | |
return INVALID_FILENAME_CHARS.sub('-', filename) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment