(filename)
return '%s (%d).%s' % (name, idx, ext)
Expands name portion of filename with numeric ' (x)' suffix to
    return filename that doesn't exist already.
Expands name portion of filename with numeric ' (x)' suffix to
    return filename that doesn't exist already.
Expands name portion of filename with numeric ( x ) suffix to return filename that doesn t exist already .
def filename_fix_existing(filename):
    """Expands name portion of filename with numeric ' (x)' suffix to
    return filename that doesn't exist already.
    """
    dirname = '.' 
    name, ext = filename.rsplit('.', 1)
    names = [x for x in os.listdir(dirname) if x.startswith(name)]
    names = [x.rsplit('.', 1)[0] for x in names]
    suffixes = [x.replace(name, '') for x in names]
    # filter suffixes that match ' (x)' pattern
    suffixes = [x[2:-1] for x in suffixes
                   if x.startswith(' (') and x.endswith(')')]
    indexes  = [int(x) for x in suffixes
                   if set(x) <= set('0123456789')]
    idx = 1
    if indexes:
        idx += sorted(indexes)[-1]
    return '%s (%d).%s' % (name, idx, ext)
def filename_fix_existing ( filename ) : dirname = '.' name , ext = filename . rsplit ( '.' , 1 ) names = [ x for x in os . listdir ( dirname ) if x . startswith ( name ) ] names = [ x . rsplit ( '.' , 1 ) [ 0 ] for x in names ] suffixes = [ x . replace ( name , '' ) for x in names ] # filter suffixes that match ' (x)' pattern suffixes = [ x [ 2 : - 1 ] for x in suffixes if x . startswith ( ' (' ) and x . endswith ( ')' ) ] indexes = [ int ( x ) for x in suffixes if set ( x ) <= set ( '0123456789' ) ] idx = 1 if indexes : idx += sorted ( indexes ) [ - 1 ] return '%s (%d).%s' % ( name , idx , ext )