os.path.split 和 os.path.realpath

python自動化接口測試裏面經常用到的兩個os.path方法

os.path.split(path ) : 把路徑分割成 dirname(路徑名) 和 basename(文件名),返回一個元組;

os.path.realpath(path):獲取path的絕對路徑;

os.path.realpath(__file__):獲取realpath方法所在腳本的絕對路徑

 

舉例如下:

path = os.path.split(os.path.realpath(__file__))[0]

這裏os.path.realpath(__file__) 返回當前腳本的絕對路徑(比如是C:/users/automation/test.py);

os.path.split( ):將上面得到的絕對路徑進行分割得到一個元組['C:/users/automation/', 'test.py']

在後面加上一個[0],即得到了‘C:/users/automation/’

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章