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 ...
随机推荐
- Homebrew -- mac 缺失包补充工具
https://brew.sh/index_zh-cn.htmlhttps://brew.sh/ 非root权限下运行 # github 源代码 https://github.com/Homebrew ...
- 【转载】 Jointwave零延时视频传输for FPGA/ASIC进入军工领域
半导体知识产权H.264/H.265 硅IP核供应商Jointwave公司的发布了一系列视频编解码RTL IP核,已经成功应用于军事工业领域的指挥作战,无人机UAV控制,航空和航天摄像机,视频记录黑匣 ...
- MySQL中使用SHOW PROFILE命令分析性能的用法整理
show profile是由Jeremy Cole捐献给MySQL社区版本的.默认的是关闭的,但是会话级别可以开启这个功能.开启它可以让MySQL收集在执行语句的时候所使用的资源.为了统计报表,把pr ...
- Django学习经验
1.在1.9——>到2.0的版本中, Django.core.urlresolvers import reverse ——>django.urls 2.当无法访问时把原来的数据清空: 首先 ...
- hdu-1042(大数+万进制)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1042 参考文章:https://blog.csdn.net/tigerisland45/article ...
- iptables说明(转)
原文:https://www.linuxidc.com/Linux/2016-09/134832.htm 前提基础: 当主机收到一个数据包后,数据包先在内核空间中处理,若发现目的地址是自身,则传到用户 ...
- c# 得到list符合某条件的索引值,排序
请教,在List集合中怎么得到元素的索引值,参考:http://www.myexception.cn/c-sharp/385022.html 这个可以用来读取窗口的多个textbox控件中内容: -- ...
- 4-具体学习git--分支
图形的方式显示日志:git log --oneline --graph 两种方式建立分支: 1.git branch dev,建立一个dev的分支 git branch 查看分支有哪些,星号在当前分支 ...
- HTML中JavaScript调用方法
我在写web页面的时候,经常用js实现某些功能,我用的方法有两种: 1.点击调用JavaScript: <button onclick="loadXMLDoc()">b ...
- MyGeneration使用概述
1.首先要连接数据库,第一次启动myG的时候会弹出default settings对话框,以后也可以在Edit-default settings里面修改.default settings有3个tabs ...