def write_to_file_by_name(folder, fname, data, mkdir=False):
if not os.path.isdir(folder):<EOL><INDENT>if mkdir:<EOL><INDENT>preparedir(folder)<EOL><DEDENT>else:<EOL><INDENT>created = preparedir(folder, False)<EOL>if not created:<EOL><INDENT>raise ValueError("<STR_LIT>" % folder)<EOL><DEDENT><DEDENT><DEDENT>file_path = os.path.join(folder, fname)<EOL>with open(file_path, writemode) as outf:<EOL><INDENT>try:<EOL><INDENT>outf.write(str(data))<EOL>return file_path<EOL><DEDENT>except Exception as e:<EOL><INDENT>raise IOError("<STR_LIT>" % (fname, str(e)))<EOL><DEDENT><DEDENT>
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).