欢迎来到代码驿站!

Python代码

当前位置:首页 > 软件编程 > Python代码

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

上一篇:Python fileinput模块如何逐行读取多个文件

栏    目:Python代码

下一篇:对Python中内置异常层次结构详解

本文标题:python获取当前文件路径以及父文件路径的方法

本文地址:http://www.codeinn.net/misctech/105153.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有