simplebench.utils.filenames moduleπ
Utility functions for file names.
- simplebench.utils.filenames.sanitize_filename(name: str) str[source]π
Sanitizes a filename by replacing invalid characters with _ (underline).
Only a-z, A-Z, 0-9, _ (underline), and - (dash) characters are allowed. All other characters are replaced with _ and multiple sequential _ characters are then collapsed to single _ characters. Leading and trailing _ and - characters are removed.
Examples:
sanitize_filename("My File-Name.txt") # returns "My_File-Name_txt" sanitize_filename("Invalid/Chars\In:Name*?") # returns "Invalid_Chars_In_Name" sanitize_filename(" Leading and Trailing ") # returns "Leading_and_Trailing" sanitize_filename("!!!") # returns "_"
Note
This function does not check for reserved filenames on any operating system.
It is the callerβs responsibility to ensure the sanitized filename is valid for the target filesystem.
If a filename becomes completely empty after sanitization, the function will return a single underscore (β_β) character. This is the one exception to the rule that leading and trailing _ and - characters are removed.
- Parameters:
name (str) β The filename to sanitize.
- Returns:
The sanitized filename.
- Return type:
- Raises:
SimpleBenchTypeError β If the
namearg is not a str.SimpleBenchValueError β If the
namearg is an empty string.