def handleFileCollision(fileName, fileCollisionMethod):
 if (fileCollisionMethod == 'overwrite'):
  logging.warning(('Data file, %s, will be overwritten' % fileName))
 elif (fileCollisionMethod == 'fail'):
  msg = 'Data file %s already exists. Set argument fileCollisionMethod to overwrite.'
  raise IOError((msg % fileName))
 elif (fileCollisionMethod == 'rename'):
  (rootName, extension) = os.path.splitext(fileName)
  matchingFiles = glob.glob(('%s*%s' % (rootName, extension)))
  if (not matchingFiles):
   fileName = ('%s%s' % (rootName, extension))
  else:
   fileName = ('%s_%d%s' % (rootName, len(matchingFiles), extension))
  if os.path.exists(fileName):
   msg = 'New fileName %s has already been taken. Something is wrong with the append counter.'
   raise IOError((msg % fileName))
 else:
  msg = 'Argument fileCollisionMethod was invalid: %s'
  raise ValueError((msg % str(fileCollisionMethod)))
 return fileName

def handleFileCollision ( fileName , fileCollisionMethod ) : if ( fileCollisionMethod == 'overwrite' ) : logging . warning ( ( 'Data file, %s, will be overwritten' % fileName ) ) elif ( fileCollisionMethod == 'fail' ) : msg = 'Data file %s already exists. Set argument fileCollisionMethod to overwrite.' raise IOError ( ( msg % fileName ) ) elif ( fileCollisionMethod == 'rename' ) : ( rootName , extension ) = os . path . splitext ( fileName ) matchingFiles = glob . glob ( ( '%s*%s' % ( rootName , extension ) ) ) if ( not matchingFiles ) : fileName = ( '%s%s' % ( rootName , extension ) ) else : fileName = ( '%s_%d%s' % ( rootName , len ( matchingFiles ) , extension ) ) if os . path . exists ( fileName ) : msg = 'New fileName %s has already been taken. Something is wrong with the append counter.' raise IOError ( ( msg % fileName ) ) else : msg = 'Argument fileCollisionMethod was invalid: %s' raise ValueError ( ( msg % str ( fileCollisionMethod ) ) ) return fileName
handle filename collisions by overwriting .