os.path.dirname(__file__)
os.path.dirname(__file__) 返回脚本的路径
描述:
- 必须实际存在的.py文件,如果直接在命令行执行,则会引发异常NameError: name 'file' is not defined;
- 在运行的时候如果输入完整的执行路径,则返回.py文件的全路径如:/Users/gokaniku/PycharmProjects/qa-autotest/os_path_test.py 则返回/Users/gokaniku/PycharmProjects/qa-autotest,如果os_path_test.py则返回空;
- 结合os.path.abspath()使用效果会好;
实例:

import os path1 = os.path.dirname(__file__)
print (path1) path2 = os.path.dirname(os.path.dirname(__file__)) #
print(path2)#获取当前运行脚本的绝对路径(去掉最后一个路径) path3 = os.path.dirname(os.path.dirname(os.path.dirname(__file__)))
print(path3)#获取当前运行脚本的绝对路径(去掉最后2个路径) path4 = os.__file__
print(path4)#获取os库的绝对路径 path5 = os.path.split(os.path.realpath(__file__))[0]
print(path5) path6 = os.path.join(os.path.split(os.path.realpath(__file__))[0],'os_path_test.py')
print(path6)

返回结果:

/Users/gokaniku/.pyenv/shims/python3 /Users/gokaniku/PycharmProjects/qa-autotest/os_path_test.py
/Users/gokaniku/PycharmProjects/qa-autotest
/Users/gokaniku/PycharmProjects
/Users/gokaniku/.pyenv/versions/3.7.2/lib/python3.7/os.py
/Users/gokaniku/PycharmProjects/qa-autotest
/Users/gokaniku/PycharmProjects/qa-autotest/os_path_test.py

os.path.dirname(__file__)的更多相关文章
- python中os.path.dirname(__file__) 命令行 参数没有绝对路径导致数据库找不到
(1).当"print os.path.dirname(__file__)"所在脚本是以完整路径被运行的, 那么将输出该脚本所在的完整路径,比如: python d:/python ...
- python中的os.path.dirname(__file__)的使用
在编程时,我们要获取当前文件所在的路径,以适合所有的工程,建立相对路径. python的os.path.dirname(__file__)非常好用,建议大家使用: import os FILE = o ...
- Python os.path.dirname(__file__) 与 Python os.path.abspath(__file__) 与 os.system() 函数
Python os.path.dirname(__file__) 与 Python os.path.abspath(__file__) 的区别 os.path.abspath(__file__)返回 ...
- 转: Python中的os.path.dirname(__file__)
(1).当"print os.path.dirname(__file__)"所在脚本是以完整路径被运行的, 那么将输出该脚本所在的完整路径,比如: ...
- Python os.path.dirname(__file__) os.path.join(str,str)
Python os.path.dirname(__file__) Python os.path.join(str,str) (1).当"print os.path.dirname(__f ...
- os.path.dirname(__file__)使用
os.path.dirname(__file__)使用 该测试脚本所在的位置:D:\第1层\第2层\第3层\第4层\第5层\test11.py test11.py import os #该文件所在位置 ...
- Python——os.path.dirname(__file__) 与 os.path.join(str,str)
Python os.path.dirname(__file__) Python os.path.join(str,str) (1).当"print os.path.dirname(__f ...
- python中的os.path.dirname与os.path.dirname(__file__)的用法
python中的os.path.dirname的用法 os.path.dirname(path) 语法:os.path.dirname(path) 功能:去掉文件名,返回目录 如: print(os. ...
- os.path.dirname(__file__)使用、Python os.path.abspath(__file__)使用
python中的os.path.dirname(__file__)的使用 - CSDN博客https://blog.csdn.net/u011760056/article/details/469698 ...
随机推荐
- Java之nio MappedByteBuffer的资源释放问题
使用nio的MappedByteBuffer映射内存, 在最后执行File.delete()方法的时候, 返回false, 即文件没有被删除. 原因是MappedByteBuffer在内存中也会创建 ...
- HTTP的请求方法一共有9种,有OPTIONS, HEAD, GET, POST等等(消息头有图,十分清楚)
请求方法:指定了客户端想对指定的资源/服务器作何种操作 下面我们介绍HTTP/1.1中可用的请求方法: [GET:获取资源] GET方法用来请求已被URI识别的资源.指定的资源经服务器端解析后 ...
- 图像滤镜艺术---ZPhotoEngine超级算法库
原文:图像滤镜艺术---ZPhotoEngine超级算法库 一直以来,都有个想法,想要做一个属于自己的图像算法库,这个想法,在经过了几个月的努力之后,终于诞生了,这就是ZPhotoEngine算法库. ...
- C#基础加强篇—委托、Lambda表达式和事件(下)
3.事件 事件作为C#中的一种类型,为类和类的实例定义发出通知的能力,从而将事件和可执行代码捆绑在了一起.事件是对象发送的消息,以发信号通知操作的发生.操作可能是由用户交互引起的,也可能是由某些其他的 ...
- 使用sklearn构建含有标量属性的决策树
网络上使用sklearn生成决策树的资料很多,这里主要说明遇见标量数据的处理. 经查验参考资料,sklearn并非使用了课上以及书上讲的ID3算法,而是选择了CART,该算法生成二叉树:scikit- ...
- Python爬虫: "追新番"网站资源链接爬取
“追新番”网站 追新番网站提供最新的日剧和日影下载地址,更新比较快. 个人比较喜欢看日剧,因此想着通过爬取该网站,做一个资源地图 可以查看网站到底有哪些日剧,并且随时可以下载. 资源地图 爬取的资源地 ...
- C# 屏蔽Ctrl Alt Del 快捷键方法+屏蔽所有输入
原文:C# 屏蔽Ctrl Alt Del 快捷键方法+屏蔽所有输入 Win32.cs /* * * FileCreate By Bluefire * Used To Import WindowsApi ...
- 简单的Windows Webcam应用:Barcode Reader
原文:简单的Windows Webcam应用:Barcode Reader 在Windows上用WinForm创建一个Webcam应用需要用到DirectShow.DirectShow没有提供C#的接 ...
- How to Move SSL certificate from Apache to Tomcat
https://www.sslsupportdesk.com/how-to-move-ssl-certificate-from-apache-to-tomcat/ Apache uses x509 p ...
- 支付宝RSA签名之Delphi实现
Delphi有个很大的问题就是,厂商的不作为(没有封装标准的Cipher类库),让大家自己造轮子. 今天的轮子就是RSA签名,由于Delphi没有封装Cipher类库,所以只的自己写了. 因为要在Fi ...