r"""
    Returns the longest root of _path that exists

    Args:
        _path (str):  path string

    Returns:
        str: _path -  path string

    CommandLine:
        python -m utool.util_path --exec-longest_existing_path

    Example:
        >>> # ENABLE_DOCTEST
        >>> from utool.util_path import *  # NOQA
        >>> import utool as ut
        >>> target = dirname(ut.__file__)
        >>> _path = join(target, 'nonexist/foobar')
        >>> existing_path = longest_existing_path(_path)
        >>> result = ('existing_path = %s' % (str(existing_path),))
        >>> print(result)
        >>> assert existing_path == target

def longest_existing_path(_path):
    r"""
    Returns the longest root of _path that exists

    Args:
        _path (str):  path string

    Returns:
        str: _path -  path string

    CommandLine:
        python -m utool.util_path --exec-longest_existing_path

    Example:
        >>> # ENABLE_DOCTEST
        >>> from utool.util_path import *  # NOQA
        >>> import utool as ut
        >>> target = dirname(ut.__file__)
        >>> _path = join(target, 'nonexist/foobar')
        >>> existing_path = longest_existing_path(_path)
        >>> result = ('existing_path = %s' % (str(existing_path),))
        >>> print(result)
        >>> assert existing_path == target
    """
    existing_path = _path
    while True:
        _path_new = os.path.dirname(existing_path)
        if exists(_path_new):
            existing_path = _path_new
            break
        if _path_new == existing_path:
            print('!!! [utool] This is a very illformated path indeed.')
            existing_path = ''
            break
        existing_path = _path_new
    return existing_path