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
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:
......
Markdown is supported
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