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. Nodejs+Mongo+WebAPI

    Nodejs+Mongo+WebAPI集成 1.[ 目录]: |- models/bear.js |- node_modules/ |- express |- mongoose |- body-par ...

  2. python之常用模块篇5

    一.日志模块,logging模块 1)logging模块简单使用,屏幕输出.默认级别30 import logging logging.debug( logging.info( logging.war ...

  3. virtual、abstract、interface区别以及用法

    virtual 用于在基类中的使用的方法,使用的情况为:      情况1:在基类中定义了virtual方法,但在派生类中没有重写该虚方法.那么在对派生类实例的调用中,该虚方法使用的是基类定义的方法. ...

  4. 使用 jfreechart 生成 曲线、柱状图、饼状图、分布图 展示到JSP

    虽然现在JS做报表和图形展示已经非常普遍和漂亮了,但是不能忽略有jfreechart 这样一种东西! 这些翻阅资料,在看以前写的示例时发现了关于jfreechart 的简单示例,不管怎样发上来分享一下 ...

  5. Cisco interview

    A.  1. Self-introduction I am Yanlin He . I am a master degree candidate of school of infomation sci ...

  6. 2019.01.21 bzoj1758: [Wc2010]重建计划(01分数规划+长链剖分+线段树)

    传送门 长链剖分好题. 题意简述:给一棵树,问边数在[L,R][L,R][L,R]之间的路径权值和与边数之比的最大值. 思路: 用脚指头想都知道要01分数规划. 考虑怎么checkcheckcheck ...

  7. js保存,获取,删除cookie的操作

    [转]来自:http://blog.csdn.net/itmyhome1990/article/details/7363816 JSP表单页面 <form action="login& ...

  8. Day1-Python基础--数据类型

    距离上次更新,已经一月有余.说明学习状态不好,且滞后严重.第二模块也滞后5周之多,可能学习方法不对,有点凌乱,导致写作业时思路还是打不开,再一个是练习的太少了吧,以后得多挤挤时间来了.目前到了这个年纪 ...

  9. 20180705 fragment

    https://www.cnblogs.com/chaowang/p/6180825.html https://blog.csdn.net/xxkalychen/article/details/537 ...

  10. ssh+注解开发 pom.xml

    <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/20 ...