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

本文详细介绍了Python自动化接口测试中os.path模块的两个常用方法:os.path.split()和os.path.realpath()。通过实例展示了如何使用这两个方法来处理文件路径,包括分割路径和获取绝对路径。
4277





