您好,登錄后才能下訂單哦!
os.path.join()函數:連接兩個或更多的路徑名組件
1.如果各組件名首字母不包含'/',則函數會自動加上
demo1
import os Path2 = 'home' Path3 = 'develop' Path4 = 'code' Path20 = Path2 + Path3 + Path4 Path30 = os.path.join(Path2,Path3,Path4) print ('Path20 = ',Path20) print ('Path30 = ',Path30)
輸出:
Path20 = homedevelopcode
Path30 = home\develop\code
2.如果有一個組件是一個絕對路徑,則在它之前的所有組件均會被舍棄
demo2
import os Path2 = '/home' Path3 = 'develop' Path4 = 'code' Path20 = Path2 + Path3 + Path4 Path30 = os.path.join(Path2, Path3, Path4) Path40 = os.path.join(Path3, Path2, Path4) print('Path20 = ',Path20) print('Path30 = ',Path30) print('Path40 = ',Path40)
輸出:
Path20 = /homedevelopcode
Path30 = /home\develop\code
Path40 = /home\code
3.如果最后一個組件為空,則生成的路徑以一個'/'分隔符結尾
demo3
import os Path2 = 'home' Path3 = 'develop' Path4 = '' Path20 = Path2 + Path3 + Path4 Path30 = os.path.join(Path2, Path3, Path4) Path40 = os.path.join(Path3, Path2, Path4) print('Path20 = ',Path20) print('Path30 = ',Path30)
輸出:
Path20 = homedevelop
Path30 = home\develop\
總結
以上所述是小編給大家介紹的python路徑拼接os.path.join()函數的用法,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對億速云網站的支持!
如果你覺得本文對你有幫助,歡迎轉載,煩請注明出處,謝謝!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。