os.path.dirname(__file__)使用

该测试脚本所在的位置:D:\第1层\第2层\第3层\第4层\第5层\test11.py

test11.py

  1. import os
  2. #该文件所在位置:D:\第1层\第2层\第3层\第4层\第5层\test11.py
  3. path1 = os.path.dirname(__file__)
  4. print(path1)#获取当前运行脚本的绝对路径
  5. path2 = os.path.dirname(os.path.dirname(__file__)) #
  6. print(path2)#获取当前运行脚本的绝对路径(去掉最后一个路径)
  7. path3 = os.path.dirname(os.path.dirname(os.path.dirname(__file__)))
  8. print(path3)#获取当前运行脚本的绝对路径(去掉最后2个路径)
  9. path4 = os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(__file__))))
  10. print(path4)#获取当前运行脚本的绝对路径(去掉最后3个路径)
  11. path5 = os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(__file__)))))
  12. print(path5)#获取当前运行脚本的绝对路径(去掉最后4个路径)
  13. path6 = os.__file__                  #获取os所在的目录
  14. print(path6)

结果:

  1. C:\Python352\python.exe D:/第1层/第2层/第3层/第4层/第5层/test11.py
  2. D:/第1层/第2层/第3层/第4层/第5层
  3. D:/第1层/第2层/第3层/第4层
  4. D:/第1层/第2层/第3层
  5. D:/第1层/第2层
  6. D:/第1层
  7. C:\Python352\lib\os.py
  8. Process finished with exit code 0

解释:

http://blog.csdn.net/u011760056/article/details/46969883

os.path.dirname(__file__)返回脚本的路径,但是需要注意一下几点:

  • 必须是实际存在的.py文件,如果在命令行执行,则会引发异常NameError: name '__file__' is not defined;
  • 在运行的时候如果输入完整的执行的路径,则返回.py文件的全路径如:Python c:/test/test.py 则返回路径 c:/test ,如果是python test.py 则返回空;
  • 结合os.path.abspath用,效果会好,如果大家看过一些python架构的代码的话,会发现经常有这样的组合:os.path.dirname(os.path.abspath(__file__)),os.path.abspath(__file__)返回的是.py文件的绝对路径。

这就是os.path.dirname(__file__)的用法,其主要总结起来有:

  • 不要在命令行的形式来进行os.path.dirname(__file__)这种形式来使用这个函数;
  • 结合os.path.abspath()使用

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

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

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

  8. os.path.dirname(__file__)

    os.path.dirname(__file__) 返回脚本的路径 描述: 必须实际存在的.py文件,如果直接在命令行执行,则会引发异常NameError: name 'file' is not de ...

  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. DOM操作二三事

    我突然想起了append(),但是我记不太清它是原生JS的还是jQuery封装的,貌似是JS的,咦?那它在jQuery里叫什么来着?哎呀!记不清了!确定append()是JS里的?不是jQuery里的 ...

  2. 【BZOJ】2099: [Usaco2010 Dec]Letter 恐吓信

    [题意]给定长度为n和m的两个字符串S和T,要求在字符串S中取出若干段拼成T(可重复取),求最小段数,n,m<=50000. [算法]后缀自动机 || 后缀数组 [题解]对串S建SAM,然后在上 ...

  3. 利用PCA可视化异常点

    异常点往往是由于某一个特征或者多个特征数值异常.但是对于多维度特征无法直接进行可视化观测异常点,利用PCA技术进行维度缩减,可以在二维或者三维空间上进行可视化展示. 原数据如下: from sklea ...

  4. 【Explain】mysql之explain详解(分析索引的最佳使用)

    在日常工作中,我们会有时会开慢查询去记录一些执行时间比较久的SQL语句,找出这些SQL语句并不意味着完事了,些时我们常常用到explain 这个命令来查看一个这些SQL语句的执行计划,查看该SQL语句 ...

  5. s3c6410下移植sqlite3.7.8

    http://blog.chinaunix.NET/uid-30441-id-2133838.html 1.下载源代码http://www.sqlite.org/download.html 2.tar ...

  6. shell读取文件的每一行内容并输出【转】

    写法一: #!/bin/bash while read line do echo $line done < file(待读取的文件) 写法二: #!/bin/bash cat file(待读取的 ...

  7. elk系列1之入门安装与基本操作【转】

    preface 我们每天都要查看服务器的日志,一方面是为了开发的同事翻找日志,另一方面是巡检服务器查看日志,而随着服务器数量以及越来越多的业务上线,日志越来越多,人肉运维相当痛苦了,此时,参考现在非常 ...

  8. 关于Java代码优化的35条建议

    代码优化,一个很重要的课题.可能有些人觉得没用,一些细小的地方有什么好修改的,改与不改对于代码的运行效率有什么影响呢?这个问题我是这么考虑的,就像大海里面的鲸鱼一样,它吃一条小虾米有用吗?没用,但是, ...

  9. gradle-4.1-rc-1-all.zip gradle-4.1-rc-2-all.zip 免费下载(百度网盘)

    今天下载遇到官网不给力,给有需要的你 gradle-4.1-rc-1-all.zip 密码: uyey gradle-4.1-rc-2-all.zip 密码: txhh

  10. u-boot启动第二阶段以及界面命令分析

    u-boot第一阶段完成了一些平台相关的硬件的配置,第一阶段所做的事情也是为第二阶段的准备,我们知道在第一阶段最后时搭建好C运行环境,之后调用了start_armboot(),那么很显然第二阶段从st ...