replace_file_extension()

audeer.replace_file_extension(path, new_extension, *, ext=None)[source]

Replace file extension.

If ext is None it uses audeer.file_extension() to identify the current extension and replaces it with new_extension.

If ext is not None but path ends on a different extension, the original path is returned.

Parameters
  • path (Union[str, bytes]) – path to file

  • new_extension (str) – new file extension

  • ext (Optional[str]) – explicit extension to be removed

Return type

str

Returns

path to file with a possibly new extension

Examples

>>> replace_file_extension("file.txt", "rst")
'file.rst'
>>> replace_file_extension("file", "rst")
'file.rst'
>>> replace_file_extension("file.txt", "")
'file'
>>> replace_file_extension("file.tar.gz", "zip", ext="tar.gz")
'file.zip'
>>> replace_file_extension("file.zip", "rst", ext="txt")
'file.zip'