Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Evan Edmond
fslpy
Commits
84118ac7
Commit
84118ac7
authored
Apr 16, 2021
by
Paul McCarthy
🚵
Browse files
RF: path.add/remove/splitExt supports pathlib
parent
48a1cda1
Changes
1
Hide whitespace changes
Inline
Side-by-side
fsl/utils/path.py
View file @
84118ac7
...
@@ -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
:
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment