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__)"所在脚本是以完整路径被运行的, 那么将输出该脚本所在的完整路径,比如: ...
随机推荐
- direct2d封装
图片项目
- python string 之 format
python 在 2.6之后支持了string的format功能, 很强大, 应该成为使用习惯. http://blog.csdn.net/handsomekang/article/details/9 ...
- 如何优雅的使用RabbitMQ(转)
RabbitMQ无疑是目前最流行的消息队列之一,对各种语言环境的支持也很丰富,作为一个.NET developer有必要学习和了解这一工具.消息队列的使用场景大概有3种: 1.系统集成,分布式系统的设 ...
- 从数据库导出数据到excel之POI操作
项目说明: 1:数据库中有两张表,主键关联 2:根据条件查询数据 3:处理为需要的数据封装类型,然后传到导出excel的方法中 <--框架部署就不详谈了,用的spring框架--> 补充: ...
- 【转】理解Linux 配置文件
原文网址:http://www.mike.org.cn/articles/understanding-linux-configuration-files-linux/ 介绍每个 Linux 程序都是一 ...
- QT creator 调试问题
问题:debug出现“ptrace:不允许的操作.” 解决办法: # may not be appropriate for developers or servers with only admin ...
- 相关TableLayoutPanel分页显示自定义控件
public partial class AcrossGrid : UserControl { /// <summary> /// 一页数量 /// </summary> ; ...
- Java语言与C语言混合编程(2)--在Java中调用C语言本地库
在上一篇文章中介绍了Java语言中的native关键字,以及Java语言调用C语言的编译生成本地动态链接库(DLL)实现加法运算的小例子,本文通过一个更加详细的例子,深入讲解Java语言调用C语言的函 ...
- appium历史版本下载地址
https://github.com/appium/appium-desktop/releases
- java代码----------实现创建DataInputStream和DataOutputStream进行读写
总结: 主要是 捕获异常 package com.a.b; import java.io.*; public class testData { public static void main(Stri ...