get_unique_filename

converter.get_unique_filename(path)

Generate a unique file path by appending “_n” before the file extension if needed.

If the given path already exists, this function appends an incrementing number before the file extension (e.g., “file_1.py”) until an unused filename is found.

Parameters

Name Type Description Default
path Union[str, Path] The initial file path to check. required

Returns

Name Type Description
Path A unique file path that does not currently exist.

Examples

>>> get_unique_filename("script.py")
PosixPath('script.py')  # if 'script.py' does not exist
>>> get_unique_filename("script.py")
PosixPath('script_1.py')  # if 'script.py' exists