#NEWLINE# def increment_index_in_filename( filename ) : #NEWLINE# #INDENT# '\n    filename should be in the form file.ext or file-2.ext - we check for the\n    dash and index and increment appropriately\n    ' #NEWLINE# regex = re . compile ( '(.+?)\\-(\\d+)(\\..+)' ) #NEWLINE# match = regex . match ( filename ) #NEWLINE# if match : #NEWLINE# #INDENT# basename = match . groups ( ) [ 0 ] #NEWLINE# index = ( int ( match . groups ( ) [ 1 ] ) + 1 ) #NEWLINE# ext = match . groups ( ) [ 2 ] #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# index = 1 #NEWLINE# ( basename , ext ) = os . path . splitext ( filename ) #NEWLINE# #UNINDENT# new_filename = ( '%s-%d%s' % ( basename , index , ext ) ) #NEWLINE# return new_filename