diff --git a/fsl/utils/path.py b/fsl/utils/path.py index 0a0859528945a7b944482442b0ffc658d0dc05f9..9ffc15c934061cf701e375c4c6fa6d0e13e8563a 100644 --- a/fsl/utils/path.py +++ b/fsl/utils/path.py @@ -32,11 +32,17 @@ import os.path as op import os import glob import operator +import pathlib import re +from typing import Sequence, Tuple, Union + from fsl.utils.platform import platform +PathLike = Union[str, pathlib.Path] + + class PathError(Exception): """``Exception`` class raised by the functions defined in this module when something goes wrong. @@ -223,19 +229,29 @@ def addExt(prefix, return allPaths[0] -def removeExt(filename, allowedExts=None, firstDot=False): +def removeExt( + filename : PathLike, + allowedExts : Sequence[str] = None, + firstDot : bool = False +) -> str: """Returns the base name of the given file name. See :func:`splitExt`. """ - return splitExt(filename, allowedExts, firstDot)[0] -def getExt(filename, allowedExts=None, firstDot=False): +def getExt( + filename : PathLike, + allowedExts : Sequence[str] = None, + firstDot : bool = False +) -> str: """Returns the extension of the given file name. See :func:`splitExt`. """ - return splitExt(filename, allowedExts, firstDot)[1] -def splitExt(filename, allowedExts=None, firstDot=False): +def splitExt( + filename : PathLike, + allowedExts : Sequence[str] = None, + firstDot : bool = False +) -> Tuple[str, str]: """Returns the base name and the extension from the given file name. If ``allowedExts`` is ``None`` and ``firstDot`` is ``False``, this @@ -262,6 +278,8 @@ def splitExt(filename, allowedExts=None, firstDot=False): last period. Ignored if ``allowedExts`` is specified. """ + filename = str(filename) + # If allowedExts is not specified # we split on a period character if allowedExts is None: