Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
F
fslpy
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Deploy
Releases
Container Registry
Model registry
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Michiel Cottaar
fslpy
Commits
a35946ea
Commit
a35946ea
authored
5 years ago
by
Paul McCarthy
Browse files
Options
Downloads
Patches
Plain Diff
ENH: new "firstDot" option to addget/remove/splitExt, for general
double-barrelled filename support (but not HCP files :( )
parent
581400f2
No related branches found
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
fsl/utils/path.py
+35
-11
35 additions, 11 deletions
fsl/utils/path.py
with
35 additions
and
11 deletions
fsl/utils/path.py
+
35
−
11
View file @
a35946ea
...
...
@@ -218,37 +218,61 @@ def addExt(prefix,
return
allPaths
[
0
]
def
removeExt
(
filename
,
allowedExts
=
None
):
def
removeExt
(
filename
,
allowedExts
=
None
,
firstDot
=
False
):
"""
Returns the base name of the given file name. See :func:`splitExt`.
"""
return
splitExt
(
filename
,
allowedExts
)[
0
]
return
splitExt
(
filename
,
allowedExts
,
firstDot
)[
0
]
def
getExt
(
filename
,
allowedExts
=
None
):
def
getExt
(
filename
,
allowedExts
=
None
,
firstDot
=
False
):
"""
Returns the extension of the given file name. See :func:`splitExt`.
"""
return
splitExt
(
filename
,
allowedExts
)[
1
]
return
splitExt
(
filename
,
allowedExts
,
firstDot
)[
1
]
def
splitExt
(
filename
,
allowedExts
=
None
):
def
splitExt
(
filename
,
allowedExts
=
None
,
firstDot
=
False
):
"""
Returns the base name and the extension from the given file name.
If ``allowedExts`` is ``None``, this function is equivalent to using::
If ``allowedExts`` is ``None`` and ``firstDot`` is ``False``, this
function is equivalent to using::
os.path.splitext(filename)
If ``allowedExts`` is provided, but the file does not end with an allowed
extension, a tuple containing ``(filename,
''
)`` is returned.
If ``allowedExts`` is ``None`` and ``firstDot`` is ``True``, the file
name is split on the first period that is found, rather than the last
period. For example::
splitExt(
'
image.nii.gz
'
) # -> (
'
image.nii
'
,
'
.gz
'
)
splitExt(
'
image.nii.gz
'
, firstDot=True) # -> (
'
image
'
,
'
.nii.gz
'
)
If ``allowedExts`` is provided, ``firstDot`` is ignored. In this case, if
the file does not end with an allowed extension, a tuple containing
``(filename,
''
)`` is returned.
:arg filename: The file name to split.
:arg allowedExts: Allowed/recognised file extensions.
:arg firstDot: Split the file name on the first period, rather than the
last period. Ignored if ``allowedExts`` is specified.
"""
# If allowedExts is not specified
,
# we
just use op.splitext
# If allowedExts is not specified
# we
split on a period character
if
allowedExts
is
None
:
return
op
.
splitext
(
filename
)
# split on last period - equivalent
# to op.splitext
if
not
firstDot
:
return
op
.
splitext
(
filename
)
# split on first period
else
:
idx
=
filename
.
find
(
'
.
'
)
if
idx
==
-
1
:
return
filename
,
''
else
:
return
filename
[:
idx
],
filename
[
idx
:]
# Otherwise, try and find a suffix match
extMatches
=
[
filename
.
endswith
(
ext
)
for
ext
in
allowedExts
]
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment