def sanitize_filename(filename):
    """
    Removes illegal characters from a string in order to obtain
    a valid file name

    :param filename: non-sanitized filename
    :return: sanitized filename
    :rtype: str
    """
    keep_characters = (' ', '.', '_')
    return "".join(c for c in filename if c.isalnum() or c in keep_characters).rstrip()