python返回相对父目录

2024-04-19 20:18:25 发布

您现在位置:Python中文网/ 问答频道 /正文

我从多台计算机的云目录运行脚本,因此出于各种原因需要访问相对路径以将输出保存在其他各种相对路径中。我希望下面的脚本返回云目录的根目录,但它返回的是none类型。有人能帮我理解为什么吗?你知道吗

def get_relative_path(base_folder_name, current_path):
     import os

     parent = os.path.split(current_path)[0]
     print base_folder_name, os.path.split(current_path)[1]
     print type(base_folder_name), type(os.path.split(current_path)[1])

     if os.path.split(current_path)[1] == base_folder_name:
          return current_path
     else:
          get_relative_path(base_folder_name, parent)

import os
path = os.getcwd()
x = get_relative_path('cloud_folder', path)
print type(x)

脚本返回:

cloud_folder child_folder
<type 'str'> <type 'str'>
cloud_folder cloud_folder
<type 'str'> <type 'str'>
<type 'NoneType'>

我希望它返回一个字符串,其中包含云文件夹的相对路径。为什么会这样?你知道吗


Tags: pathname目录脚本cloudbasegetos