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. 疯狂的图形(利用C# + GDI plus模拟杂乱无章的现实场景)

    原文:疯狂的图形(利用C# + GDI plus模拟杂乱无章的现实场景) 本文给出了模拟竹叶.长叶草.杂乱石头.天上繁星等关键代码.使用.Net环境下C#语言,GDI+编写.   模拟竹叶 挺像的吧? ...

  2. Win8 Metro(C#)数字图像处理--2.70修正后的阿尔法滤波器

    原文:Win8 Metro(C#)数字图像处理--2.70修正后的阿尔法滤波器 /// <summary> /// Alpha filter. /// </summary> / ...

  3. HTML5离线缓存攻击测试(二)

    经过昨天的测试,发现使用离线缓存的网站会被攻击.但是,不使用离线缓存的网站就真的不会受到这样的攻击么? 据我理解,按照标准当浏览器请求manifest文件时,若没有请求到,或者文件发生改变,应当不使用 ...

  4. 错误代码: 0x800700b7 配置错误:定义了重复的“system.web.extensions/scripting/scriptResourceHandler”节

    操作环境:Windows 8.1  IIS8.5 VS2008 解决方法: 把应用程序池的.Net CLR 版本从4.0修改为2.0即可.如下图所示:

  5. Markdown 入门

    一. Markdown语法的简要规则 标题 标题是非常重要的一个标记,一段文字标记为标题,只需要在文字前加 #.具体可以支持到1到6个# 1 2 3 4 # 一级标题 ## 二级标题 ### 三级标题 ...

  6. 微信小程序把玩(三十八)获取设备信息 API

    原文:微信小程序把玩(三十八)获取设备信息 API 获取设备信息这里分为四种, 主要属性: 网络信息wx.getNetWorkType, 系统信息wx.getSystemInfo, 重力感应数据wx. ...

  7. 创建dll动态链接库,并使用java调用

    参考文章:http://www.cnblogs.com/matthew-2013/p/3480296.html http://blog.csdn.net/g710710/article/details ...

  8. mysq练习(二)

    Mysql练习(二) 1. delete,drop,truncate 的区别? 可以参考这位的:  https://www.cnblogs.com/zhizhao/p/7825469.html 2. ...

  9. 数据库连接池之_DButils

    // 这个是在添加数据 @Test public void demo1() { QueryRunner qr = new QueryRunner(JDBCUtils.getDataSource()); ...

  10. 使用Chart控件进行实时监控

    Chart作为微软提供绘制图表的控件,在刚开始使用时非常的迷茫,因为功能强大,涉及到的知识多, 一开始难以接收过来,但后天经过查找资料,耐心学习,终于还是有了一定的收获. Chart相当于一个大的图纸 ...