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 ...
随机推荐
- postgresql 自带函数
替换函数 SELECT replace('abcdefabcdef', 'cd', 'XX') 得到 abXXefabXXef ------------------------------------ ...
- WPF ListView控件设置奇偶行背景色交替变换以及ListViewItem鼠标悬停动画
原文:WPF ListView控件设置奇偶行背景色交替变换以及ListViewItem鼠标悬停动画 利用WPF的ListView控件实现类似于Winform中DataGrid行背景色交替变换的效果,同 ...
- WPF 4 Ribbon 开发 之 标签工具栏(Tab Toolbar)
原文:WPF 4 Ribbon 开发 之 标签工具栏(Tab Toolbar) 本篇将开始介绍标签工具栏的开发内容,标签工具栏可以说是Ribbon 的核心部分,应用程序所有的功能特性都会集中 ...
- WPF 多路绑定
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threa ...
- SQL SERVER LEAD和LAG使用
示例:获取在48小时之内重复的记录 SELECT * FROM ( SELECT b.* , LAG(b.OperatorTime, , b.OperatorTime) OVER ( PARTITIO ...
- dotnetspider
http://www.cnblogs.com/modestmt/p/5525467.html nuget :DotnetSpider2.Core
- JVM的几个介绍
关于jvm内存的几点 jvm在运行时分为方法区(Method Area) .虚拟机栈(VM Stack).本地方法栈(Native Method Stack).堆 (Heap).程序计数器 (Prog ...
- Capsule Network
Capsule Network最大的特色在于vector in vector out & 动态路由算法. vector in vector out 所谓vector in vector out ...
- jQuery实时聊天jquery-chat
jquery-chat是基于jQuery UI + Node.js + Socket.IO 实现100%纯JavaScript实时聊天(客户端和服务器都是JS),实现了facebook / Gmail ...
- BI-学习之 商业智能项目工具安装
首先咱们先需要下载一个工具,Microsoft SQL Server Data Tools - Business Intelligence for Visual Studio 2012并安装: 我目前 ...