os.path.join()

os.path.join()
函數功能:鏈接兩個或更多的路徑名組件函數

若是各組件名首字母不包含’/’,則函數會自動加上spa

若是有一個組件是一個絕對路徑,則在它以前的全部組件均會被捨棄code

若是最後一個組件爲空,則生成的路徑以一個’/’分隔符結尾blog

Demo1

import os

Path1 = 'home'
Path2 = 'develop'
Path3 = 'code'

Path10 = Path1 + Path2 + Path3
Path20 = os.path.join(Path1,Path2,Path3)
print ('Path10 = ',Path10)
print ('Path20 = ',Path20) 

 

輸出以下:class

Path10 =  homedevelopcode
Path20 =  home\develop\code

Demo2

import os

Path1 = '/home'
Path2 = 'develop'
Path3 = 'code'

Path10 = Path1 + Path2 + Path3
Path20 = os.path.join(Path1,Path2,Path3)
print ('Path10 = ',Path10)
print ('Path20 = ',Path20) 

 

輸出以下:import

Path10 =  /homedevelopcode
Path20 =  /home\develop\code

 

Demo3

import os

Path1 = 'home'
Path2 = '/develop'
Path3 = 'code'

Path10 = Path1 + Path2 + Path3
Path20 = os.path.join(Path1,Path2,Path3)
print ('Path10 = ',Path10)
print ('Path20 = ',Path20) 

 

輸出以下:im

Path10 =  home/developcode
Path20 =  /develop\code

 

Demo4

import os

Path1 = 'home'
Path2 = 'develop'
Path3 = '/code'

Path10 = Path1 + Path2 + Path3
Path20 = os.path.join(Path1,Path2,Path3)
print ('Path10 = ',Path10)
print ('Path20 = ',Path20) 

 

輸出以下:di

Path10 =  homedevelop/code
Path20 =  /code
相關文章
相關標籤/搜索