os中的路径获取方法
#当前文件的路径
pwd = os.getcwd() # 绝对路径
pwd = Path(os.getcwd()).as_posix()
#当前文件的父路径(绝对路径)
father_path1=os.path.abspath(os.path.dirname(pwd)+os.path.sep+".")
#当前文件的父路径
father_path2=os.path.dirname(pwd)
print(pwd)
print(father_path1)
print(father_path2)
结果如下:
D:/Alian/pyqt-GUI/Laser_diseases-GUI
D:\Alian\pyqt-GUI
D:/Alian/pyqt-GUI
其他代码实现(转载)
http://www.cppcns.com/jiaoben/python/265057.html
def TestPrtPwd(self):
print("获取当前文件路径——" + os.path.realpath(__file__)) # 获取当前文件路径
parent = os.path.dirname(os.path.realpath(__file__))
print("获取其父目录——" + parent) # 从当前文件路径中获取目录
garder = os.path.dirname(parent)
print("获取父目录的父目录——" + garder)
print("获取文件名" + os.path.basename(os.path.realpath(__file__))) # 获取文件名
# 当前文件的路径
pwd = os.getcwd()
print("当前运行文件路径" + pwd)
# 当前文件的父路径
father_path = os.path.abspath(os.path.dirname(pwd) + os.path.sep + ".")
print("运行文件父路径" + father_path)
# 当前文件的前两级目录
grader_father = os.path.abspath(os.path.dirname(pwd) + os.path.sep + "..")
print("运行文件父路径的父路径" + grader_father)
return garder
结果如下:
获取当前文件路径——D:\SVN\测试\autotest\functionalAutomation\aonr_sxsj\AuditData\common\redConfig.py
获取其父目录——D:\SVN\测试\autotest\functionalAutomation\aonr_sxsj\AuditData\common
获取父目录的父目录——D:\SVN\测试\autotest\functionalAutomation\aonr_sxsj\AuditData
获取文件名redConfig.py
当前运行文件路径D:\SVN\测试\autotest\functionalAutomation\aonr_sxsj\AuditData\TestSuite\RoleManagement
运行文件父路径D:\SVN\测试\autotest\functionalAutomation\aonr_sxsj\AuditData\TestSuite
运行文件父路径的父路径D:\SVN\测试\autotest\functionalAutomation\aonr_sxsj\AuditData