NiBabel

Access a cacophony of neuro-imaging file formats

Previous topic

nibabel.filename_parser.types_filenames

Next topic

nibabel.filename_parser.splitext_addext

This Page

Reggie -- the one

nibabel.filename_parser.parse_filename

nibabel.filename_parser.parse_filename(filename, types_exts, trailing_suffixes, match_case=False)

Splits filename into tuple of (fileroot, extension, trailing_suffix, guessed_name)

Parameters :

filename : str

filename in which to search for type extensions

types_exts : sequence of sequences

sequence of (name, extension) str sequences defining type to extension mapping.

trailing_suffixes : sequence of strings

suffixes that should be ignored when looking for extensions

match_case : bool, optional

If True, match case of extensions and trailing suffixes when searching in filename, otherwise do case-insensitive match.

Returns :

pth : str

path with any matching extensions or trailing suffixes removed

ext : str

If there were any matching extensions, in types_exts return that; otherwise return extension derived from os.path.splitext.

trailing : str

If there were any matching trailing_suffixes return that matching suffix, otherwise ‘’

guessed_type : str

If we found a matching extension in types_exts return the corresponding type

Examples

>>> types_exts = (('t1', 'ext1'),('t2', 'ext2'))
>>> parse_filename('/path/fname.funny', types_exts, ())
('/path/fname', '.funny', None, None)
>>> parse_filename('/path/fnameext2', types_exts, ())
('/path/fname', 'ext2', None, 't2')
>>> parse_filename('/path/fnameext2', types_exts, ('.gz',))
('/path/fname', 'ext2', None, 't2')
>>> parse_filename('/path/fnameext2.gz', types_exts, ('.gz',))
('/path/fname', 'ext2', '.gz', 't2')