os.path.dirname( __ file __ ) 2018/6/2
os.path.dirname( __ file __ ) 2018/6/2
该测试脚本所在的位置:D:\第1层\第2层\第3层\第4层\第5层\test11.py
import os
#该文件所在位置:D:\第1层\第2层\第3层\第4层\第5层\test11.py
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.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(__file__))))
print(path4)#获取当前运行脚本的绝对路径(去掉最后3个路径)
path5 = os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(os.path.dirname(__file__)))))
print(path5)#获取当前运行脚本的绝对路径(去掉最后4个路径)
path6 = os.__file__ #获取os所在的目录
print(path6)
运行的结果:
C:\Python352\python.exe D:/第1层/第2层/第3层/第4层/第5层/test11.py
D:/第1层/第2层/第3层/第4层/第5层
D:/第1层/第2层/第3层/第4层
D:/第1层/第2层/第3层
D:/第1层/第2层
D:/第1层
C:\Python352\lib\os.py
Process finished with exit code 0
- 解释:
- os.path.dirname( __ file__ )返回脚本的路径,但是需要注意一下几点:
1、必须是实际存在的.py文件,如果在命令行执行,则会引发异常NameError: name '__ file__' is not defined
2、在运行的时候如果输入完整的执行的路径,则返回.py文件的全路径如:
python c:/test/test.py 则返回路径 c:/test ,如果是python test.py 则返回空
3、结合os.path.abspath用,效果会好,如果大家看过一些python架构的代码的话,会发现经常有这样的组合
os.path.dirname(os.path.abspath(__ file__ )), 其中os.path.abspath(__ file__)返回的是.py文件的绝对路径
这就是os.path.dirname(__ file__)的用法,其主要总结起来有:
1、不要已命令行的形式来进行os.path.dirname( file)这种形式来使用这个函数
2、结合os.path.abspath()使用
os.path.dirname( __ file __ ) 2018/6/2的更多相关文章
- python中的os.path.dirname(__file__)的使用
在编程时,我们要获取当前文件所在的路径,以适合所有的工程,建立相对路径. python的os.path.dirname(__file__)非常好用,建议大家使用: import os FILE = o ...
- Python3基础 os.path.dirname 对路径字符串进行处理 返回所在文件夹的路径
Python : 3.7.0 OS : Ubuntu 18.04.1 LTS IDE : PyCharm 2018.2.4 Conda ...
- 第十八篇 模块与包--time&random模块&模块导入import(os.path.dirname(os.path.abspath(__file__)))
模块 在Python中, 一个.py文件就称为一个模块. 使用模块的好处: 1. 最大的好处就是大大提高了代码的可维护性 2. 编写代码不必从零开始.一个模块编写完毕,就可以被其他地方引用.在写其他程 ...
- os.path.dirname(__file__)
os.path.dirname(__file__) 返回脚本的路径 描述: 必须实际存在的.py文件,如果直接在命令行执行,则会引发异常NameError: name 'file' is not de ...
- 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 ...
- python中os.path.dirname(__file__) 命令行 参数没有绝对路径导致数据库找不到
(1).当"print os.path.dirname(__file__)"所在脚本是以完整路径被运行的, 那么将输出该脚本所在的完整路径,比如: python d:/python ...
- os.path.dirname使用方法
import os path1=os.path.abspath(__file__) print(path1)#当前文件的绝对路径 path2=os.path.dirname(os.path.abspa ...
- 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__)"所在脚本是以完整路径被运行的, 那么将输出该脚本所在的完整路径,比如: ...
随机推荐
- flask第八篇——url_for【1】
我们已经知道,知道了url就可以找到对应的视图函数,那么现在问题来了,如果我们知道了视图函数,要怎么找到url呢?这时候我们就需要url_for函数了. # coding: utf-8 from fl ...
- ofdm理解(转载)
说明:以下文字,灰色为吹水文,黑色为正文,蓝色为采用实际应用中的参数所作的说明. 起因是这样的.时间回到07年底,4G方兴之时,同桌隔壁的隔壁"小白"同学说看不太明白OFDMA的原 ...
- GridView合并表头多重表头
后台代码: using System; using System.Data; using System.Configuration; using System.Web; using System.We ...
- java 并发synchronized使用
从版本1.0开始,java中每个对象都有一个内部锁,如果一个方法用synchronized修饰,那么对象的锁将保护整个方法,也就是说要调用该方法,线程必须获得内部的对象锁 换句话说 public sy ...
- element popover 不显示/不隐藏问题解决方法
html代码部分 <el-table-column label="操作"> <template slot-scope="scope"> ...
- 利用 netsh 给 mysql 开启多端口监听
利用 netsh 给 mysql 开启多端口监听 标题党,实际并不是真的多端口监听,只是端口转发而已. 由于某种特殊原因需要 mysql 服务器多个端口监听. mysql 服务器本身是不支持的,但可以 ...
- c#实现QQ群成员列表导出及邮件群发之邮件群发
主题已迁移至:http://atiblogs.com/ ITO-神奇的程序员
- 3069: [Pa2011]Hard Choice 艰难的选择
Description Byteasar是一个很纠结的人.每次他经过Bytetown的时候都知道有至少2条不同的路径可以选择,这导致他必须花很长时间来决定走哪条路.Byteasar最近听说了Bytet ...
- Account银行账户
package com.hanqi; //账户类 public class Account { String ZhangHao; double CunKuanYuE; Account(String Z ...
- sklearn: TfidfVectorizer 中文处理及一些使用参数
TfidfVectorizer可以把原始文本转化为tf-idf的特征矩阵,从而为后续的文本相似度计算,主题模型,文本搜索排序等一系列应用奠定基础.基本应用如: #coding=utf-8 from s ...