replace_file_extension()

audformat.utils.replace_file_extension(index, extension, pattern=None)[source]

Change the file extension of index entries.

It replaces all existing file extensions in the index file path by the new provided one.

Parameters
  • index (Index) – index with file path conform to table specifications

  • extension (str) – new file extension without '.'. If set to '', the current file extension is removed

  • pattern (Optional[str]) – regexp pattern to match current extensions. In contrast to extension, you have to include '.'. If None the default of r'\.[a-zA-Z0-9]+$' is used

Return type

Index

Returns

updated index

Examples

>>> index = filewise_index(["f1.wav", "f2.flac"])
>>> replace_file_extension(index, "mp3")
Index(['f1.mp3', 'f2.mp3'], dtype='string', name='file')
>>> index = filewise_index(["f1.wav.gz", "f2.wav.gz"])
>>> replace_file_extension(index, "")
Index(['f1.wav', 'f2.wav'], dtype='string', name='file')
>>> replace_file_extension(index, "flac", pattern=r"\.wav\.gz$")
Index(['f1.flac', 'f2.flac'], dtype='string', name='file')