def write_to(fpath, to_write, aslines=False, verbose=None,
             onlyifdiff=False, mode='w', n=None):
    if onlyifdiff:
        import utool as ut
        if ut.hashstr(read_from(fpath)) == ut.hashstr(to_write):
            print('[util_io] * no difference')
            return
    verbose = _rectify_verb_write(verbose)
    if verbose:
        print('[util_io] * Writing to text file: {}'.format(fpath))
    backup = False and exists(fpath)
    if backup:
        util_path.copy(fpath, fpath + '.backup')
    if not isinstance(fpath, six.string_types):
        fpath = fpath.name
    with open(fpath, mode) as file_:
        if aslines:
            file_.writelines(to_write)
        else:
            if six.PY2:
                if isinstance(to_write, unicode):
                    to_write = to_write.encode('utf8')
            try:
                file_.write(to_write)
            except UnicodeEncodeError as ex:
                start = max(ex.args[2] - 10, 0)
                end = ex.args[3] + 10
                context = to_write[start:end]
                print(repr(context))
                print(context)
                from utool import util_dbg
                util_dbg.printex(ex, keys=[(type, 'to_write')])
                file_.close()
                if backup:
                    util_path.copy(fpath + '.backup', fpath)
                raise