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__)的更多相关文章

  1. python中os.path.dirname(__file__) 命令行 参数没有绝对路径导致数据库找不到

    (1).当"print os.path.dirname(__file__)"所在脚本是以完整路径被运行的, 那么将输出该脚本所在的完整路径,比如: python d:/python ...

  2. python中的os.path.dirname(__file__)的使用

    在编程时,我们要获取当前文件所在的路径,以适合所有的工程,建立相对路径. python的os.path.dirname(__file__)非常好用,建议大家使用: import os FILE = o ...

  3. 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__)返回 ...

  4. 转: Python中的os.path.dirname(__file__)

     (1).当"print os.path.dirname(__file__)"所在脚本是以完整路径被运行的, 那么将输出该脚本所在的完整路径,比如:                 ...

  5. 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 ...

  6. os.path.dirname(__file__)使用

    os.path.dirname(__file__)使用 该测试脚本所在的位置:D:\第1层\第2层\第3层\第4层\第5层\test11.py test11.py import os #该文件所在位置 ...

  7. 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 ...

  8. python中的os.path.dirname与os.path.dirname(__file__)的用法

    python中的os.path.dirname的用法 os.path.dirname(path) 语法:os.path.dirname(path) 功能:去掉文件名,返回目录 如: print(os. ...

  9. os.path.dirname(__file__)使用、Python os.path.abspath(__file__)使用

    python中的os.path.dirname(__file__)的使用 - CSDN博客https://blog.csdn.net/u011760056/article/details/469698 ...

随机推荐

  1. Golang的演化历程

    本文来自Google的Golang语言设计者之一Rob Pike大神在GopherCon2014大会上的开幕主题演讲资料“Hello, Gophers!”.Rob大神在这次分 享中用了两个生动的例子讲 ...

  2. 【C#/WPF】调节图像的HSL(色相、饱和度、明亮度)

    原文:[C#/WPF]调节图像的HSL(色相.饱和度.明亮度) 先说概念: HSL是一种描述颜色的方式(其他颜色描述方式还有大家熟悉的RGB值).HSL三个字母分别表示图像的Hue色相.Saturat ...

  3. XF相对布局

    using System; using Xamarin.Forms; using Xamarin.Forms.Xaml; [assembly: XamlCompilation (XamlCompila ...

  4. EF 两种删除方式的比较

    UserInfo user = from u in context.UserInfo where u.Id=343 select u; context.UserInfo.Remove(user); 用 ...

  5. BuildWinRTL.dproj 用这个重新编译就行

    BuildWinRTL.dproj 用这个重新编译就行 我每次安装新版本,都删掉了这两个函数 {$IFDEF DEBUG}exports  dbkFCallWrapperAddr,{$IF defin ...

  6. 多进程demo

    多进程实现DOS重定向输出,界面如下: 主要的代码实现如下: #define MAXREADBUFFERLEN (60 * 1000) void CRedirectDlg::OnBnClickedBu ...

  7. qt获取网络ip地址的类

    最近在学习qt网络编程,基于tcp和udp协议. 看了一些别人的程序和qt4自带的例子,困扰我最大的问题就是获取ip的类,总结起来还挺多的. 主要介绍常用的QtNetwork Module中的QHos ...

  8. 95+强悍的jQuery图形效果插件

    现在的网站越来越离不开图形,好的图像效果能让你的网站增色不少.通过JQuery图形效果插件可以很容易的给你的网站添加一些很酷的效果. 使用JQuery插件其实比想象的要容易很多,效果也超乎想象.在本文 ...

  9. Http请求的响应没有Content-Length,只有Transfer-Encoding→chunked

    如题:Http请求的响应没有Content-Length,只有Transfer-Encoding→chunked.如图 原因猜测:如果请求的响应返回是某个对象,则不会显示Content-Length, ...

  10. 台电P89s mini root教程

    根据论坛内的一些内容再结合自己的使用心得整理如下,本人双11购入P89s mini root成功  自带软件什么的都不见了 以下是个人root过程,有不一样的地方欢迎交流,说实话我也不是很懂 1.升级 ...