uniquename#
- uniquename(name=None, namelist=None, style=None, human=False, suffix=None)[source]#
Given a name and a list of other names, add a counter to the name so that it doesn’t conflict with the other names.
Useful for auto-incrementing filenames, etc.
- Parameters:
name (str) – the string to ensure is unique
namelist (list) – the list of strings that are taken
style (str) – a custom style for appending the counter
human (bool) – if True, use ‘ (%d)’ as the style instead of ‘%d’
suffix (str) – if provided, remove this suffix from each name and add it back to the unique name
Examples:
sc.uniquename('out', ['out', 'out1']) # Returns 'out2' sc.uniquename(name='file', namelist=['file', 'file (1)', 'file (2)', 'myfile'], human=True) # Returns 'file (3)' sc.uniquename('results.csv', ['results.csv', 'results1.csv'], suffix='.csv') # Returns 'results2.csv'
New in version 3.2.0: “human” and “suffix” arguments, simpler default style