def clean_end_spaces(text):
    """ Remove spaces at the end of lines

    >>> clean_end_spaces("a \\n  \\n    b \\n")
    'a\\n\\n    b\\n'
    """
    while " \n" in text:
        text = text.replace(" \n", "\n")
    while text and text[-1] == " ":
        text = text[:-1]
    return text