python获取当前文件路径以及父文件路径的方法
时间:2021-04-20 08:58:04|栏目:Python代码|点击: 次

#当前文件的路径 pwd = os.getcwd() #当前文件的父路径 father_path=os.path.abspath(os.path.dirname(pwd)+os.path.sep+".") #当前文件的前两级目录 grader_father=os.path.abspath(os.path.dirname(pwd)+os.path.sep+"..")
追加部分代码实例
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







