一、os.path.realpath(file)
path = os.path.realpath(__file__)输出:D:\pythonProject\python+pytest+allure+log+yaml+mysql+钉钉企微通知接口自动化框架\main.py
二、os.path.realpath(str)
fp = os.path.realpath("2.ini")注意:这里是获取路径,无论这个文件有没有都这样返回输出:D:\pythonProject\python+pytest+allure+log+yaml+mysql+钉钉企微通知接口自动化框架\2.ini
三、os.path.split
file_path = os.path.split("D:/SoftWare/autoTest/AutoRunTest/Public/Common/ReadConfigIni.py")输出:('D:/SoftWare/autoTest/AutoRunTest/Public/Common', 'ReadConfigIni.py')
四、os.path.join
file1 = os.path.join(file_xpath,'config.ini')file_xpath: D:/python输出:D:/python/config.ini
五、os.getcwd
import os# 当前路径pwd = os.getcwd()当前路径:D:/pythonProject/python+pytest+allure+log+yaml+mysql+钉钉企微通知接口自动化框架/main.py输出:D:\pythonProject\python+pytest+allure+log+yaml+mysql+钉钉企微通知接口自动化框架# 当前路径的父路径f_pwd = os.path.abspath(os.path.dirname(os.getcwd()) + os.path.sep + ".")f_pwd = os.path.abspath(os.path.dirname(pwd))
六、os.path.dirname(file)
pwd = os.path.dirname(__file__)当前路径:D:/pythonProject/python+pytest+allure+log+yaml+mysql+钉钉企微通知接口自动化框架/main.py输出:D:/pythonProject/python+pytest+allure+log+yaml+mysql+钉钉企微通知接口自动化框架
七、os.path.exists
path = os.path.exists('main.py')输出:False或True
八、os.sep
_SLASH = os.sep作用:根据你所处的平台,自动采用相应的分割符号python是跨平台的语言,在Windows中,文件路径的分隔符为“\”,在Linux中,文件的路径的分隔符为“/”import ospath = "E:" + os.sep + "py_project" + os.sep + "linux"输出:E:\py_project\linux