Skip to content

Commit 6af67cb

Browse files
Better handle pathological filenames
os.path.splitext() does not provide the behavior we need to properly recognize edge cases such as '.nii' as image files.
1 parent 586c0e0 commit 6af67cb

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed

nibabel/filename_parser.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -264,9 +264,14 @@ def splitext_addext(filename,
264264
for ext in addexts:
265265
if endswith(filename, ext):
266266
extpos = -len(ext)
267-
addext = filename[extpos:]
268-
filename = filename[:extpos]
267+
filename, addext = filename[:extpos], filename[extpos:]
269268
break
270269
else:
271270
addext = ''
272-
return os.path.splitext(filename) + (addext,)
271+
# os.path.splitext() behaves unexpectedly when filename starts with '.'
272+
extpos = filename.rfind('.')
273+
if extpos < 0 or filename == '.':
274+
root, ext = filename, ''
275+
else:
276+
root, ext = filename[:extpos], filename[extpos:]
277+
return (root, ext, addext)

0 commit comments

Comments
 (0)