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(__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具体存放的位置。

上一篇:Python os.path.dirname(__file__) 与 Python os.path.abspath(__file__) 与 os.system() 函数


下一篇:python os.path 的使用