python获取当前文件路径以及父文件路径的方法
作者:lxting21 时间:2022-07-02 14:06:00
#当前文件的路径
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
来源:https://blog.csdn.net/lvxiaoting/article/details/80309190
标签:python,文件,父文件,路径
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Python数据类型及常用方法
2022-09-22 08:09:19
Python制作当年第一款手机游戏-贪吃蛇游戏(练习)
2022-06-20 02:37:35
![](https://img.aspxhome.com/file/2023/0/79540_0s.gif)
TensorFlow加载模型时出错的解决方式
2023-12-23 05:49:04
selenium+python实现自动化登录的方法
2021-08-07 13:38:08
Python Flask-Login实现用户会话管理
2023-06-05 13:13:50
![](https://img.aspxhome.com/file/2023/9/90589_0s.png)
GO的range具体使用
2023-07-21 03:27:27
Python self用法详解
2021-08-24 04:26:41
介绍Python中几个常用的类方法
2023-02-10 09:17:49
SQL Server数据库占用过多内存的解决方法
2009-10-23 14:02:00
python爬虫之爬取百度音乐的实现方法
2022-09-02 06:22:10
![](https://img.aspxhome.com/file/2023/0/65420_0s.jpg)
ASP如何使用CDONTS来发送电子邮件?
2010-06-05 12:35:00
python实现感知器算法(批处理)
2022-09-06 18:02:25
![](https://img.aspxhome.com/file/2023/7/91977_0s.jpg)
Django之全局使用request.user.username的实例详解
2021-07-20 20:50:42
Go语言实现新春祝福二维码的生成
2023-10-16 06:14:32
![](https://img.aspxhome.com/file/2023/0/95360_0s.png)
python eval 转换k m到乘法计算的操作
2023-10-30 03:26:04
![](https://img.aspxhome.com/file/2023/8/99158_0s.png)
Pycharm及python安装详细步骤及PyCharm配置整理(推荐)
2021-03-15 21:55:51
![](https://img.aspxhome.com/file/2023/9/76019_0s.jpg)
在Internet Explorer中正确使用MSXML
2009-02-22 18:41:00
10个有用的Python字符串函数小结
2023-08-13 11:26:28
python中用logging实现日志滚动和过期日志删除功能
2021-03-17 14:21:18
python打印当前文件的绝对路径并解决打印为空的问题
2023-10-26 16:24:00