python3 os.path.realpath(__file__) 和 os.path.cwd() 方法的区别


os.path.realpath

获取当前执行脚本的绝对路径。

os.path.realpath(__file__)

os.path.cwd()

获取当前脚本的所在路径

脚本一:

所在路径:

/Users/wangxiansheng/Documents/Pycharm/PyMySQL/insert_sql.py

import os
def getpath():
        file = os.path.realpath(__file__)
        print('insert_sql file:', file)
        cwd = os.getcwd()
        print('insert_sql cwd:', cwd)
insert_sql file: /Users/wangxiansheng/Documents/Pycharm/PyMySQL/insert_sql.py

insert_sql cwd: /Users/wangxiansheng/Documents/Pycharm/PyMySQL

脚本二:

所在路径:

/Users/wangxiansheng/Documents/Pycharm/christian/cia.py

from PyMySQL import insert_sql
import os
insert_sql.getpath()
path = os.getcwd()
print('cia cwd', path)
file = os.path.realpath(__file__)
print('cia file:', file)
insert_sql file: /Users/wangxiansheng/Documents/Pycharm/PyMySQL/insert_sql.py

insert_sql cwd: /Users/wangxiansheng/Documents/Pycharm/christian

cia cwd /Users/wangxiansheng/Documents/Pycharm/christian

cia file: /Users/wangxiansheng/Documents/Pycharm/christian/cia.py

结论:
- realpath() 获得的是该方法所在的脚本的路径
- cwd,获得的是当前执行脚本的所在路径,无论从哪里调用的该方法。

博主目前用的是Python的os.getcwd()方法,但我一位朋友给出的是os.path.dirname(os.path.realpath(__file__))

那么,这两种方式到底有什么本质区别?

博主通过具体的实验来进行解释。

先给出2个目录的结构:

(1)PycharmProjects/pythonLearn/dir/dir2/getRootPath.py

(2)PycharmProjects/pythonLearn/getPath.py

【1】那我们先看看第一个PycharmProjects/pythonLearn/dir/dir2/getRootPath.py,如下代码:

  1. import os
  2.  
  3. def getCurPath1():
  4. cur_path = os.path.dirname(os.path.realpath(__file__))
  5. return cur_path
  6.  
  7. def getCurPath2():
  8. cur_path = os.getcwd()
  9. return cur_path
  10.  
  11.  
  12. print('func1----'+getCurPath1())
  13. print('func2----'+getCurPath2())

我们直接执行该脚本得到的结果如下:

func1----C:\Users\Administrator\PycharmProjects\PythonLearn\dir\dir2

func2----C:\Users\Administrator\PycharmProjects\PythonLearn\dir\dir2

并未看出本质区别,获取的都是当前脚本所在的dir2目录。

【2】那我们再看看第二个PycharmProjects/pythonLearn/getPath.py,如下代码:

现在,我们在里面我们引入了PycharmProjects/pythonLearn/dir/dir2/目录下的getRootPath.py模块。

  1. from dir.dir2 import getRootPath
  2.  
  3. path1 = getRootPath.getCurPath1()
  4. path2 = getRootPath.getCurPath2()

直接执行getPath.py文件获取的结果如下:

func1----C:\Users\Administrator\PycharmProjects\PythonLearn\dir\dir2

func2----C:\Users\Administrator\PycharmProjects\PythonLearn

这个时候,你有没有发现有什么不同,这里的func1就是os.path.dirname(os.path.realname(__file__))获取的__file__所在脚本的路径,也就是getRootPath.py的路径。

而os.getcwd()获取的当前最外层调用的脚本路径,即getPath所在的目录也可描述为起始的执行目录,A调用B,起始的是A,那么获取的就是A所在的目录路径。

方法补充说明:

os.path.dirname():去掉脚本的文件名,返回目录。

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

python3 os.path.realpath(__file__) 和 os.path.cwd() 方法的区别的更多相关文章

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

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

  3. os.getcwd()和os.path.realpath(__file__)的区别

    https://blog.csdn.net/xiaminli/article/details/74944580 python中split().os.path.split()函数用法

  4. os.path.dirname(__file__)和os.path.abspath(__file__)区别

  5. os.path.dirname(__file__)

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

  6. 4. 获取当前的文件夹的路径,以及当前文件名的路径 os.path.realpath

    使用os.path.realpath(__file__) 获得当前的文件夹的路径名, 使用os.path.split 进行路径切割 import os src, _= os.path.split(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(__file__) 命令行 参数没有绝对路径导致数据库找不到

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

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

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

随机推荐

  1. Scrum 敏捷开发

    使用敏捷开发一个月的事件,基本的开发模式跟我遇到的这个文章介绍的基本类似,暂时简单Copy到了这里...... http://www.scrumcn.com/agile/scrum-knowledge ...

  2. jdom解析xml

    这次把代码整理了一下,打包发上来了,程序中需要用到的jar包也一起打在里面了,有兴趣的朋友可以解压后直接导入的eclipse里运行! xml是一种广为使用的可扩展标记语言,java中解析xml的方式有 ...

  3. @Valid报错 No validator could be found for constraint

    使用hibernate validator出现上面的错误, 需要 注意 @NotNull 和 @NotEmpty  和@NotBlank 区别 @NotEmpty 用在集合类上面@NotBlank 用 ...

  4. crud树型结构数据

    小型数据,比如标签,部门之类的,可以组织数据,成层状结构,一并返回前端,节省请求次数:但是大型数据,比如省市区等等联动,如果一并返回组织好的数据,查询量大,页面多次刷新,恶意请求,放入缓存还可以,其实 ...

  5. BP神经网络的理论理论常识

    BP神经网络的简单结构:输入层.一个或者多个隐层.输出层.图如下: 在图中,涉及到的参数有:X1--Xn为输入参数.输入参数通过输入层和隐层之间的的链接权重进行计算,到达隐层. 隐层的输入参数通过隐层 ...

  6. 通过html文件生成PDF文件

    /// <summary> /// 获取html内容,转成PDF(注册) /// </summary> public void DownloadPDFByHTML(string ...

  7. Mybatis-Plus 实战完整学习笔记(十)------条件构造器核心用法大全(下)

    31.升序orderByAsc 31.升序orderByAsc List<Employee> employeeList = employeeMapper.selectList(new Qu ...

  8. 关于DOM级别的一些问题,DOM0,DOM1,DOM2

    之前看书没太注意这个问题,直到我今天看书看到一个DOM0级,于是我就在群里问了下各个级别的意思区别.. 首先我们的确定标准了是没有DOM0级的.在平时阅读的时候可能会读到DOM0级(DOM Level ...

  9. ubuntu-server-12.04.2安装配置jdk

    原文链接:http://blog.csdn.net/amymengfan/article/details/9958461 我选择的是离线安装,这需要先下载好jdk安装包(下载地址:http://www ...

  10. Remote Debugging (3)

    use Eclipse| a Web application 创建一个简单的web项目 AServlet.java package cn.zno; import java.io.IOException ...