2015-01-20 52 views
0

我從幾臺計算機的雲目錄運行腳本,因爲各種原因需要訪問相對路徑以將輸出保存在其他各種相對路徑中。我預計下面的腳本會返回雲目錄的根目錄,但它會返回一個無類型的。任何人都可以幫我理解爲什麼?python返回相對父目錄

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'> 

我預期與雲文件夾中的相對路徑返回一個字符串。爲什麼是這樣?

回答

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

你需要返回其他情況下,也