os.path.join()
函數功能:鏈接兩個或更多的路徑名組件函數
若是各組件名首字母不包含’/’,則函數會自動加上spa
若是有一個組件是一個絕對路徑,則在它以前的全部組件均會被捨棄code
若是最後一個組件爲空,則生成的路徑以一個’/’分隔符結尾blog
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
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
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
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