追加部分代码实例
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以上这篇python获取当前文件路径以及父文件路径的方法就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。