您的位置:首页 > 编程语言 > Python开发

Python——os.path.dirname(__file__) 与 os.path.join(str,str)

2014-08-07 22:26 671 查看
Python os.path.dirname(__file__)
Python os.path.join(str,str)

(1).当"print os.path.dirname(__file__)"所在脚本是以完整路径被运行的, 那么将输出该脚本所在的完整路径,比如:
python d:\pythonSrc\test\test.py
那么将输出 d:\pythonSrc\test
(2).当"print os.path.dirname(__file__)"所在脚本是以相对路径被运行的, 那么将输出空目录,比如:
python test.py
那么将输出空字符串

认识下os.path.dirname(__file__)

获取路径名:os.path.dirname()

获取文件所在目录的完整路径:os.path.dirname(__file__)

在django项目中的settings.py中还可以进行如下的配置:

如配置数据库:


1 import os
2 DATABASE_ENGINE='sqlite3'
3 DATABASE_NAME=os.path.join(os.path.dirname(__file__),'myAPP/mydata.db')


其中 os.path.dirname(__file__)函数用于取出settings.py所在文件夹的位置,在用os.path.join()函数将该位置和后面指定的'myAPP/mydata.db' 字符串连接一起,实现sqlite3数据库文件mydata.db具体存放的位置。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: