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/’

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