Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/340.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
基于给定路径替换节点路径并在Python中附加路径的其余部分_Python_Python 3.x - Fatal编程技术网

基于给定路径替换节点路径并在Python中附加路径的其余部分

基于给定路径替换节点路径并在Python中附加路径的其余部分,python,python-3.x,Python,Python 3.x,我需要一些帮助来找出如何在python 3.x中最好地实现这一点: (注意:这不是操作系统路径结构。它是zookeeper的路径/节点结构) 输出是 /Windows/kinu-test/Windows/kinu-dev/path1/path2 我希望输出是(将前2个文件夹替换为基本路径,因为它有2个文件夹深) /Windows/Kinu测试/path1/path2 第1个路径(基本路径)中的任何路径都应附加相对路径,并根据基本路径替换相对路径长度 另一个例子 如果我的基本路径是/Windows

我需要一些帮助来找出如何在python 3.x中最好地实现这一点:

(注意:这不是操作系统路径结构。它是zookeeper的路径/节点结构)

输出是

/Windows/kinu-test/Windows/kinu-dev/path1/path2

我希望输出是(将前2个文件夹替换为基本路径,因为它有2个文件夹深)

/Windows/Kinu测试/path1/path2

第1个路径(基本路径)中的任何路径都应附加相对路径,并根据基本路径替换相对路径长度

另一个例子

如果我的基本路径是
/Windows/kinu-test/base\u 01
,相对路径是
/Windows/kinu-dev/path1/path2
,那么最终路径应该是
/Windows/kinu-test/base\u 01/path2

如果base只是
/
,那么最终路径应该是
/Windows/kinu dev/path1/path2


提前谢谢

在同事的帮助下-能够回答我自己的问题。希望这有助于未来的游客:

def join_paths(base_path, relative_paths):

    # Taking care of first case where base_path is "/" root
    if base_path ==  "/":
        return relative_paths
    # Convert the base_path to a list by splitting at "/" ["", "folder1", "folder2"]
    base_path_list = base_path.split("/")
    # Convert the relative_paths to a list by splitting at "/" ["", "folder1", "folder2"]
    relative_paths_list = relative_paths.split("/")
    # Take difference between length of base_path_list and length of relative_paths_list
    len_diff = len(relative_paths_list) - len(base_path_list)
    # Validate if difference in length is greater than 0 else it wont find 
    # the sub list it needs to append
    if len_diff > 0:
        # Get last 'n' elements from relative_paths_list where n is len_diff
        list_to_append = relative_paths_list[-len_diff:]
        # Join the base path list with sub-list we just created above
        final_list = base_path_list + list_to_append
        # Join the elements in list with "/"
        return "/".join(final_list)
     elif len_diff == 0:
        final_list = base_path_list
        return "/".join(final_list)
     else:
        print("SOmething is really wrong")

# run the code 
print(join_paths("/Windows/kin_test/path2", "/Windows/kin_dev/path2/path3"))

链接:

谢谢@wwii。我将更新我的问题,这不是操作系统文件夹。这是zookeeper节点路径。如果你能举例说明如何完成我正在努力做的事情,我将不胜感激。我确实看过操作系统包。
def join_paths(base_path, relative_paths):

    # Taking care of first case where base_path is "/" root
    if base_path ==  "/":
        return relative_paths
    # Convert the base_path to a list by splitting at "/" ["", "folder1", "folder2"]
    base_path_list = base_path.split("/")
    # Convert the relative_paths to a list by splitting at "/" ["", "folder1", "folder2"]
    relative_paths_list = relative_paths.split("/")
    # Take difference between length of base_path_list and length of relative_paths_list
    len_diff = len(relative_paths_list) - len(base_path_list)
    # Validate if difference in length is greater than 0 else it wont find 
    # the sub list it needs to append
    if len_diff > 0:
        # Get last 'n' elements from relative_paths_list where n is len_diff
        list_to_append = relative_paths_list[-len_diff:]
        # Join the base path list with sub-list we just created above
        final_list = base_path_list + list_to_append
        # Join the elements in list with "/"
        return "/".join(final_list)
     elif len_diff == 0:
        final_list = base_path_list
        return "/".join(final_list)
     else:
        print("SOmething is really wrong")

# run the code 
print(join_paths("/Windows/kin_test/path2", "/Windows/kin_dev/path2/path3"))