code: def write(self, output, mode="w", keep_rc=False):
          
          if isinstance(output, six.string_types):
              already_exists = os.path.exists(output)
              try:
                  with open(output, mode) as f:
                      p = self._build_pipes(f)
                      rc = p.wait()
                      if keep_rc:
                          return rc
                      if rc:
                          raise CalledProcessError(rc, self.cmds[0], "")
              except BaseException as be:
                  if not already_exists and os.path.exists(output):
                      os.remove(output)
                  six.reraise(be.__class__, be, sys.exc_info()[2])
          else:
              p = self._build_pipes(output)
              rc = p.wait()
              if keep_rc:
                  return rc
              if rc:
                  raise CalledProcessError(rc, self.cmds[0], "")
docs: Executes the pipeline and writes the results to the supplied output.
          If output is a filename and the file didn't already exist before trying
          to write, the file will be removed if an exception is raised.
  
          Args:
              output (str or file like object): will create a new file of this
                  name or overwrite an existing file. If output is already a file
                  like object, it is used.
              mode (str): mode to use when creating or opening the provided file
                  name if it is a string. Ignored if output is a file like object.
  
          Returns:
              The final output of the pipeline.
          Raises:
              CalledProcessError if any return code in the pipeline is nonzero.