hfutils.utils.ext
This module provides utilities for handling file extensions, particularly for files with composite extensions. It extends the standard os.path.splitext functionality to support multi-part file extensions.
splitext_with_composite
- hfutils.utils.ext.splitext_with_composite(filename, composite_extensions)[source]
Split a filename into a pair (root, ext) where ext is a composite extension if it matches one of the provided composite extensions, otherwise behaves like os.path.splitext.
This function is particularly useful when dealing with files that have multi-part extensions (e.g., ‘.tar.gz’, ‘.config.json’) where standard os.path.splitext would only split at the last dot.
- Parameters:
filename (str) – The filename to split.
composite_extensions (list[str] or tuple[str]) – A sequence of composite extensions to check against (e.g., [‘.tar.gz’, ‘.config.json’]). The matching is case-insensitive.
- Returns:
A tuple containing the root and the extension. If the filename ends with any of the composite extensions, the extension will be the full composite extension. Otherwise, returns the result of os.path.splitext.
- Return type:
tuple[str, str]
- Example:
>>> splitext_with_composite('file.tar.gz', ['.tar.gz']) ('file', '.tar.gz') >>> splitext_with_composite('file.config.json', ['.config.json']) ('file', '.config.json') >>> splitext_with_composite('file.txt', ['.tar.gz']) ('file', '.txt')