一、os.path.realpath(file)

  1. path = os.path.realpath(__file__)
  2. 输出:D:\pythonProject\python+pytest+allure+log+yaml+mysql+钉钉企微通知接口自动化框架\main.py

二、os.path.realpath(str)

  1. fp = os.path.realpath("2.ini")
  2. 注意:这里是获取路径,无论这个文件有没有都这样返回
  3. 输出:D:\pythonProject\python+pytest+allure+log+yaml+mysql+钉钉企微通知接口自动化框架\2.ini

三、os.path.split

  1. file_path = os.path.split("D:/SoftWare/autoTest/AutoRunTest/Public/Common/ReadConfigIni.py")
  2. 输出:('D:/SoftWare/autoTest/AutoRunTest/Public/Common', 'ReadConfigIni.py')

四、os.path.join

  1. file1 = os.path.join(file_xpath,'config.ini')
  2. file_xpath: D:/python
  3. 输出:D:/python/config.ini

五、os.getcwd

  1. import os
  2. # 当前路径
  3. pwd = os.getcwd()
  4. 当前路径:D:/pythonProject/python+pytest+allure+log+yaml+mysql+钉钉企微通知接口自动化框架/main.py
  5. 输出:D:\pythonProject\python+pytest+allure+log+yaml+mysql+钉钉企微通知接口自动化框架
  6. # 当前路径的父路径
  7. f_pwd = os.path.abspath(os.path.dirname(os.getcwd()) + os.path.sep + ".")
  8. f_pwd = os.path.abspath(os.path.dirname(pwd))

六、os.path.dirname(file)

  1. pwd = os.path.dirname(__file__)
  2. 当前路径:D:/pythonProject/python+pytest+allure+log+yaml+mysql+钉钉企微通知接口自动化框架/main.py
  3. 输出:D:/pythonProject/python+pytest+allure+log+yaml+mysql+钉钉企微通知接口自动化框架

七、os.path.exists

  1. path = os.path.exists('main.py')
  2. 输出:False或True

八、os.sep

  1. _SLASH = os.sep
  2. 作用:根据你所处的平台,自动采用相应的分割符号
  3. python是跨平台的语言,在Windows中,文件路径的分隔符为“\”,在Linux中,文件的路径的分隔符为“/”
  4. import os
  5. path = "E:" + os.sep + "py_project" + os.sep + "linux"
  6. 输出:E:\py_project\linux