def remove_trailing_whitespace(line):
    """Removes trailing whitespace, but preserves the newline if present.
    """
    if line.endswith("\n"):
        return "{}\n".format(remove_trailing_whitespace(line[:-1]))
    return line.rstrip()