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 in parentheses before the file extension (e.g., “file (1).py”) until an unused filename is found.

Parameters

Name Type Description Default
path str or 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