'Writes a string to a file.
It first shows a unified diff between the old text and the new text, and
then rewrites the file; the latter is only done if the write option is
set.'

def write_file(self, new_text, filename, old_text, encoding=None):

    try:
        f = _open_with_encoding(filename, 'w', encoding=encoding)
    except OSError as err:
        self.log_error("Can't    create    %s:    %s", filename, err)
        return
    try:
        f.write(_to_system_newlines(new_text))
    except OSError as err:
        self.log_error("Can't    write    %s:    %s", filename, err)
    finally:
        f.close()
    self.log_debug('Wrote    changes    to    %s', filename)
    self.wrote = True
