Commit 84118ac7 authored by Paul McCarthy's avatar Paul McCarthy 🚵
Browse files

RF: path.add/remove/splitExt supports pathlib

parent 48a1cda1
...@@ -32,11 +32,17 @@ import os.path as op ...@@ -32,11 +32,17 @@ import os.path as op
import os import os
import glob import glob
import operator import operator
import pathlib
import re import re
from typing import Sequence, Tuple, Union
from fsl.utils.platform import platform from fsl.utils.platform import platform
PathLike = Union[str, pathlib.Path]
class PathError(Exception): class PathError(Exception):
"""``Exception`` class raised by the functions defined in this module """``Exception`` class raised by the functions defined in this module
when something goes wrong. when something goes wrong.
...@@ -223,19 +229,29 @@ def addExt(prefix, ...@@ -223,19 +229,29 @@ def addExt(prefix,
return allPaths[0] 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`. """ """Returns the base name of the given file name. See :func:`splitExt`. """
return splitExt(filename, allowedExts, firstDot)[0] 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`. """ """Returns the extension of the given file name. See :func:`splitExt`. """
return splitExt(filename, allowedExts, firstDot)[1] 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. """Returns the base name and the extension from the given file name.
If ``allowedExts`` is ``None`` and ``firstDot`` is ``False``, this If ``allowedExts`` is ``None`` and ``firstDot`` is ``False``, this
...@@ -262,6 +278,8 @@ def splitExt(filename, allowedExts=None, firstDot=False): ...@@ -262,6 +278,8 @@ def splitExt(filename, allowedExts=None, firstDot=False):
last period. Ignored if ``allowedExts`` is specified. last period. Ignored if ``allowedExts`` is specified.
""" """
filename = str(filename)
# If allowedExts is not specified # If allowedExts is not specified
# we split on a period character # we split on a period character
if allowedExts is None: if allowedExts is None:
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment