<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def write_to_file_by_name(folder, fname, data, mkdir=False):
 """Write a string of data to file by filename and folder. Args: folder: Target folder (e.g. c:/ladybug). fname: File name (e.g. testPts.pts). data: Any data as string. mkdir: Set to True to create the directory if doesn't exist (Default: False). """

    if not os.path.isdir(folder):
        if mkdir:
            preparedir(folder)
        else:
            created = preparedir(folder, False)
            if not created:
                raise ValueError("Failed to find %s." % folder)

    file_path = os.path.join(folder, fname)

    with open(file_path, writemode) as outf:
        try:
            outf.write(str(data))
            return file_path
        except Exception as e:
            raise IOError("Failed to write %s to file:\n\t%s" % (fname, str(e)))