import os

path1=os.path.abspath(__file__)
print(path1)#当前文件的绝对路径 path2=os.path.dirname(os.path.abspath(__file__))
print(path2)#当前文件的上一层目录的绝对路径 path3=os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
print(path3)#当前文件的上上层目录的绝对路径 curpath = os.path.dirname(os.path.realpath(__file__)) # 当前文件夹的路径

其中

语法:os.path.dirname(path)

功能:去掉文件名,返回目录

__file__ 为内置属性,表示当前文件的path

os.path.dirname((__file__) :指的是,得到当前文件的绝对路径,是去掉脚本的文件名,只返回目录。

os.path.dirname(os,path.realname(__file__)):指的是,获得你刚才所引用的模块所在的绝对路径

os.path.dirname使用方法的更多相关文章

  1. Python3基础 os.path.dirname 对路径字符串进行处理 返回所在文件夹的路径

             Python : 3.7.0          OS : Ubuntu 18.04.1 LTS         IDE : PyCharm 2018.2.4       Conda ...

  2. Python模块详解以及import本质,获得文件当前路径os.path.abspath,获得文件的父目录os.path.dirname,放到系统变量的第一位sys.path.insert(0,x)

    模块介绍 1.定义: 模块:用来从逻辑上组织python代码(变量,函数,类,逻辑:实现一个功能),本质就是.py结尾的python文件(文件名:test.py,对应的模块名:test) 包:用来从逻 ...

  3. 第十八篇 模块与包--time&random模块&模块导入import(os.path.dirname(os.path.abspath(__file__)))

    模块 在Python中, 一个.py文件就称为一个模块. 使用模块的好处: 1. 最大的好处就是大大提高了代码的可维护性 2. 编写代码不必从零开始.一个模块编写完毕,就可以被其他地方引用.在写其他程 ...

  4. python3 获取当前路径及os.path.dirname的使用

    方法一: import sys,os os.getcwd()#然后就可以看见结果了 方法二: import os os.path.dirname(os.path.realpath('__file__' ...

  5. python os.path.dirname 是什么目录

    这个获取文件路径中所在的目录. 1 2 3 4 5 6 7 In [1]: import os   In [2]: os.__file__ Out[2]: '/usr/lib/python2.7/os ...

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

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

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

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

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

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

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

随机推荐

  1. lxml.etree.HTML(text) 解析HTML文档

    0.参考 http://lxml.de/tutorial.html#the-xml-function There is also a corresponding function HTML() for ...

  2. UIWebView的常用方法

    //webview导航栏类型enum UIWebViewNavigationType : Int { case LinkClicked case FormSubmitted case BackForw ...

  3. 关于hbase api的个人总结(带jar包)

    1.如果本机代码没问题,jar包没问题的话,检查集群也没用问题,但是代码运行显示 找不到对应的映射主机,需要在  C:\Windows\System32\drivers\etc  中修改host文件, ...

  4. echarts移动端中例子总结。

    接下来我总结一下我常用到的几个移动端echarts小例子: 第一步:我先引入自己想要的库 第二步: 给echarts给了一个窗口(有大小的窗口) 第三步: 开始引入你想要的图形的options 我做的 ...

  5. 在js中网页面写入数据时需要注意的几点

    网页代码 <tbody id="t_gun"> <s:iterator value="gunList" status="st&quo ...

  6. blur和click事件冲突

    一.问题描述文本框的blur事件和div元素的click事件出现冲突.在input的blur事件中,我们隐藏div元素.在div的click事件中,我们清除input的内容,并隐藏自身.当我们在inp ...

  7. eclipse导入新项目配置jdk、tomcat到浏览器正常访问

    好记性不如烂笔头,长时间不操作就会生疏,在此做个笔记 1.导入项目 2,进入选择项目,点finish完成,导入成功 3.配置tomcat和jdk 选择本地相对应jdk的jre 选择tomcat版本进行 ...

  8. Urozero Autumn 2016. NCPC 2016

    A. Artwork 倒过来并查集维护即可. #include<cstdio> #include<algorithm> using namespace std; const i ...

  9. Buffer --缓冲器

    一. 启动Buffer缓冲器 node 输入 buffer 创建一个新的buffer var buf = new buffer(''hello word) 查看buf的长度 buf.length 运行 ...

  10. thinkphp实现like模糊查询实例

    原网址:https://www.jb51.net/article/56846.htm