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. response输出随机图片、定时刷新网页

    第一招:利用response向浏览器输出图片: //获取验证码 在<img />标签内的src属性设为请求路径/verifyCode?goodsId=xxx&token=xxxxx ...

  2. ubuntu下使用fstab挂载硬盘时,属于root,如何把它改为属于一个用户的(如sgjm)

    http://zhidao.baidu.com/link?url=xnakfVD16EtunTSt3wBm153DyqHnXN3FSPO1E_2SpVmM5bmEIwICLA0N6zN85_ioQ3f ...

  3. 利用HBuilder开发基于MUI的H5+ app中使用百度地图定位功能

    定位功能有两种方法: 首先要初始化内置地图: var map = new plus.maps.Map("map"); 这里黄色的map是html里面的id: <div id= ...

  4. 再读c++primer plus 004

    第九章  内存模型和名称空间 1.如果文件名包含在尖括号中,则c++编译器将在存储标准头文件的主机系统的文件系统中查找,但如果文件名包含在双引号中,则编译器将首先查找当前的工作目录或源代码目录(或其他 ...

  5. kbmmw 5.04 发布

    增加了一大波功能,消灭了一大堆问题,也肯定引进了一大票BUG.We are happy to announce the release of our latest version of kbmMW. ...

  6. c# 快速排序法并记录数组索引

    在遗传算法中,只需要对适应性函数评分进行排序,没必要对所有的个体也参与排序,因为在适应性函数评分排序是可以纪律下最初的索引,排序后的索引随着元素排序而变动,这样就知道那个评分对应那个个体了: usin ...

  7. typecho 调用评论最多热门文章

    在当前主题的functions.php文件中添加以下函数代码: function getHotComments($limit = 10){ $db = Typecho_Db::get(); $resu ...

  8. 乌龙之MySQL slave IO status:connecting

    搭建了一个主从,状态一直如下: 检查错误日志报错如下: review搭建过程,语法并没有问题. 检查用户及网络,也没有问题: so?what is the cause ? 等等....貌似上面搭建用的 ...

  9. redis概览

    Redis是一个开源的使用ANSI C语言编写.支持网络.可基于内存亦可持久化的日志型.Key-Value数据库,和Memcached类似,它支持存储的value类型相对更多,包括string(字符串 ...

  10. 如何使用git提交代码

    如何使用Git管理代码 Git 是开发人员用来向代码库(msstash)中提交代码或者下载远端代码库中代码的工具. 如何使用git向代码库中提交我们修改后的代码呢? 1.如果是第一次使用git,那么需 ...