#NEWLINE# def increment_filename( inpfile , ndigits = 3 , delim = '.' ) : #NEWLINE# #INDENT# '\n    increment a data filename, returning a new (non-existing) filename\n\n       first see if a number is before \'.\'.  if so, increment it.\n       second look for number in the prefix. if so, increment it.\n       lastly, insert a \'_001\' before the \'.\', preserving suffix.\n\n    the numerical part of the file name will contain at least three digits.\n\n    >>> increment_filename(\'a.002\')\n    \'a.003\'\n    >>> increment_filename(\'a.999\')\n    \'a.1000\'\n    >>> increment_filename(\'b_017.xrf\')\n    \'b_018.xrf\'\n    >>> increment_filename(\'x_10300243.dat\')\n    \'x_10300244.dat\'\n\n    >>> increment_filename(\'x.dat\')\n    \'x_001.dat\'\n\n    >>> increment_filename(\'C:/program files/oo/data/x.002\')\n    \'C:/program files/ifeffit/data/x.003\'\n\n    >>> increment_filename(\'a_001.dat\')\n    \'a_002.dat\'\n\n    >>> increment_filename(\'a.001.dat\')\n    \'a.002.dat\'\n\n    >>> increment_filename(\'a_6.dat\')\n    \'a_007.dat\'\n\n    >>> increment_filename(\'a_001.002\')\n    \'a_001.003\'\n\n    >>> increment_filename("path/a.003")\n    \'path/a.004\'\n' #NEWLINE# ( dirname , filename ) = os . path . split ( inpfile ) #NEWLINE# ( base , ext ) = os . path . splitext ( filename ) #NEWLINE# if ( ext == '' ) : #NEWLINE# #INDENT# ext = '.000' #NEWLINE# #UNINDENT# if ext . startswith ( '.' ) : #NEWLINE# #INDENT# ext = ext [ 1 : ] #NEWLINE# #UNINDENT# if ( ndigits < 3 ) : #NEWLINE# #INDENT# ndigits = 3 #NEWLINE# #UNINDENT# form = ( '%%.%ii' % ndigits ) #NEWLINE# def _incr( base , ext ) : #NEWLINE# #INDENT# if ext . isdigit ( ) : #NEWLINE# #INDENT# ext = ( form % ( int ( ext ) + 1 ) ) #NEWLINE# #UNINDENT# else : #NEWLINE# #INDENT# found = False #NEWLINE# if ( '_' in base ) : #NEWLINE# #INDENT# parts = base . split ( '_' ) #NEWLINE# for ( iw , word ) in enumerate ( parts [ : : ( - 1 ) ] ) : #NEWLINE# #INDENT# if word . isdigit ( ) : #NEWLINE# #INDENT# parts [ ( ( len ( parts ) - iw ) - 1 ) ] = ( form % ( int ( word ) + 1 ) ) #NEWLINE# found = True #NEWLINE# break #NEWLINE# #UNINDENT# #UNINDENT# base = '_' . join ( parts ) #NEWLINE# #UNINDENT# if ( ( not found ) and ( '.' in base ) ) : #NEWLINE# #INDENT# parts = base . split ( '.' ) #NEWLINE# for ( iw , word ) in enumerate ( parts [ : : ( - 1 ) ] ) : #NEWLINE# #INDENT# if word . isdigit ( ) : #NEWLINE# #INDENT# parts [ ( ( len ( parts ) - iw ) - 1 ) ] = ( form % ( int ( word ) + 1 ) ) #NEWLINE# found = True #NEWLINE# break #NEWLINE# #UNINDENT# #UNINDENT# base = '.' . join ( parts ) #NEWLINE# #UNINDENT# if ( not found ) : #NEWLINE# #INDENT# base = ( '%s_001' % base ) #NEWLINE# #UNINDENT# #UNINDENT# return ( base , ext ) #NEWLINE# #UNINDENT# ( base , ext ) = _incr ( base , ext ) #NEWLINE# fout = pathOf ( dirname , base , ext , delim = delim ) #NEWLINE# while os . path . exists ( fout ) : #NEWLINE# #INDENT# ( base , ext ) = _incr ( base , ext ) #NEWLINE# fout = pathOf ( dirname , base , ext , delim = delim ) #NEWLINE# #UNINDENT# return fout