1、获取当前脚本路径信息
import os
#该文件所在位置:/Users/xuxinya/Documents/Git/Git1/Test_framework/test/case/test.py
#获取当前运行脚本的绝对路径
# /Users/xuxinya/Documents/Git/Git1/Test_framework/test/case/test.py
path1 = os.path.abspath(__file__)
print('1:'+path1)
#获取当前运行脚本的路径
#/Users/xuxinya/Documents/Git/Git1/Test_framework/test/case
path2 = os.path.dirname(os.path.abspath(__file__)) #
print('2:'+path2)
#获取当前运行脚本的路径
#/Users/xuxinya/Documents/Git/Git1/Test_framework/test
path3 = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
print('3:'+path3)
#获取当前运行脚本的路径
#/Users/xuxinya/Documents/Git/Git1/Test_framework
path4 = os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
print('4:'+path4)
#获取当前运行脚本的路径
#/Users/xuxinya/Documents/Git/Git1
path5 = os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.abspath(__file__)))))
print('5:'+path5)
#获取os所在的目录
# /usr/local/Cellar/python/3.7.3/Frameworks/Python.framework/Versions/3.7/lib/python3.7/os.py
path6 = os.__file__
print('6:'+path6)
2、根据当前脚本路径,获取其他路径信息
#获取本文本路径信息
#/Users/xuxinya/Documents/Git/Git1/Test_framework/test/..
base_path = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))+ '/..'
print(base_path)
#获取chromedriver路径
#/Users/xuxinya/Documents/Git/Git1/Test_framework/drivers/chromedriver
driver_path = os.path.abspath(base_path+'/drivers/chromedriver')
print(driver_path)
网友评论