<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def _create_events_writer(self, directory):
 """Creates a new events writer. Args: directory: The directory in which to write files containing events. Returns: A new events writer, which corresponds to a new events file. """

    total_size = 0
    events_files = self._fetch_events_files_on_disk()
    for file_name in events_files:
      file_path = os.path.join(self._events_directory, file_name)
      total_size += tf.io.gfile.stat(file_path).length

    if total_size >= self.total_file_size_cap_bytes:
      # The total size written to disk is too big. Delete events files until
      # the size is below the cap.
      for file_name in events_files:
        if total_size < self.total_file_size_cap_bytes:
          break

        file_path = os.path.join(self._events_directory, file_name)
        file_size = tf.io.gfile.stat(file_path).length
        try:
          tf.io.gfile.remove(file_path)
          total_size -= file_size
          logger.info(
              "Deleted %s because events files take up over %d bytes",
              file_path, self.total_file_size_cap_bytes)
        except IOError as err:
          logger.error("Deleting %s failed: %s", file_path, err)

    # We increment this index because each events writer must differ in prefix.
    self._events_file_count += 1
    file_path = "%s.%d.%d" % (
        os.path.join(directory, DEBUGGER_EVENTS_FILE_STARTING_TEXT),
        time.time(), self._events_file_count)
    logger.info("Creating events file %s", file_path)
    return pywrap_tensorflow.EventsWriter(tf.compat.as_bytes(file_path))