python3 os.path.realpath(__file__) 和 os.path.cwd() 方法的区别
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,如下代码:
- import os
- def getCurPath1():
- cur_path = os.path.dirname(os.path.realpath(__file__))
- return cur_path
- def getCurPath2():
- cur_path = os.getcwd()
- return cur_path
- print('func1----'+getCurPath1())
- 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模块。
- from dir.dir2 import getRootPath
- path1 = getRootPath.getCurPath1()
- 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() 方法的区别的更多相关文章
- 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__) 与 os.path.join(str,str)
Python os.path.dirname(__file__) Python os.path.join(str,str) (1).当"print os.path.dirname(__f ...
- os.getcwd()和os.path.realpath(__file__)的区别
https://blog.csdn.net/xiaminli/article/details/74944580 python中split().os.path.split()函数用法
- os.path.dirname(__file__)和os.path.abspath(__file__)区别
- os.path.dirname(__file__)
os.path.dirname(__file__) 返回脚本的路径 描述: 必须实际存在的.py文件,如果直接在命令行执行,则会引发异常NameError: name 'file' is not de ...
- 4. 获取当前的文件夹的路径,以及当前文件名的路径 os.path.realpath
使用os.path.realpath(__file__) 获得当前的文件夹的路径名, 使用os.path.split 进行路径切割 import os src, _= os.path.split(os ...
- 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 ...
- python中os.path.dirname(__file__) 命令行 参数没有绝对路径导致数据库找不到
(1).当"print os.path.dirname(__file__)"所在脚本是以完整路径被运行的, 那么将输出该脚本所在的完整路径,比如: python d:/python ...
- python中的os.path.dirname(__file__)的使用
在编程时,我们要获取当前文件所在的路径,以适合所有的工程,建立相对路径. python的os.path.dirname(__file__)非常好用,建议大家使用: import os FILE = o ...
随机推荐
- 【转】再讲IQueryable<T>,揭开表达式树的神秘面纱
[转]再讲IQueryable<T>,揭开表达式树的神秘面纱 接上篇<先说IEnumerable,我们每天用的foreach你真的懂它吗?> 最近园子里定制自己的orm那是一个 ...
- 58.UIScrollView XIB拖拽约束
第一步: 拖拽UIScrollView 到控制器上 ,给scrollView 添加约束 ,这时是正常的 第二步:scrollview上添加UIview ,(注意:这个 ScrollView就是根据这个 ...
- 爬虫之mongodb数据库
一 mongodb的介绍 1.易用性:mongodb是一款强大.灵活并且易扩展的数据库.他面向于文档的数据库,而不是关系性数据库.不采用关系型主要是为了获得更好的扩展性.还有一个好处就是面向文档的数据 ...
- php中@mysql_connect与mysql_connect有什么区别
屏蔽错误如果有错的话,会把语句都显示出来.加@就不显示$link=@mysql_connect('localhost','root','123') or die ("数据库连接失败" ...
- swift-基础语法2
一.整形 :有符号和无符号类型 有符号类型:Int ,Int8 ,Int32,Int64 无符号类型: UInt ,UInt8 UInt32,UInt64 注意点:如果你的开发环境是32位,那么Int ...
- 2018.11.04 洛谷P1081 开车旅行(倍增)
传送门 思路简单码量超凡? 感觉看完题大家应该都知道是倍增sbsbsb题了吧. 首先预处理出从每个点出发如果是AAA走到哪个点,如果是BBB走到哪个点. 然后利用刚刚预处理出的信息再预处理从每个点出发 ...
- poj-1328(贪心+思维)
题目链接:传送门 思路:找最少有几个点,先求出每个点的覆盖范围,就是一个点最大可以到达的地方是y相同的地方而且直线距离是d, 所以x范围在[x-sqrt(d*d-y*y),x+sqrt(d*d-y*y ...
- c#文件比较Code
我想我们很多时候想比较一个文件里面是否有改动,比如一个dll库是新加了一个方法或修改了其中的方法实现,不能通过可视化的工具来比较的时候,可以用这个小工具来比较, 以下是比较文件的代码. using S ...
- 第22章:MongoDB-聚合操作--聚合管道--$out
①$out $out:利用此操作可以将查询结果输出到指定的集合里面. ②范例:将投影的结果输出到集合里
- VIP之CVI CVO
3. VIP CVI CVO 在开始时,对于CVI和CVO是不知道应该怎样去调试的,就是不知道应该从哪里去确认是对还是错. 关于这一点从再次看到关于数据包的格式才明朗的.去分析CVI和输出 ...