from typing import List


def append_to_file(filehandle, imagefilenames: List[str], xyz_gaze: List[List[float]], target: List[List[float]],
                   prediction: List[List[float]]) -> None:
    """
    This function appends prediction data in a comma separated format to an opened file.
    Args:
        filehandle: filehandle where to append the data.
        imagefilenames: List of full path of image files.
        xyz_gaze: List of gaze in XYZ format for each of the image.
        target: List of gaze in yaw pitch format for each image.
        prediction: List of predictions in yaw pitch format for each image.

    """

    def arr_to_str(arr):
        return ','.join([str(x) for x in arr])

    output_str = ''
    for fn, gz, tr, pr in zip(imagefilenames, xyz_gaze, target, prediction):
        output_str += f'{fn},{arr_to_str(gz)},{arr_to_str(tr)},{arr_to_str(pr)}\n'
    filehandle.write(output_str)