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__) 与 os.path.join(str,str)的更多相关文章

  1. os.path.dirname(__file__)和os.path.abspath(__file__)区别

  2. python中os.path.dirname(__file__) 命令行 参数没有绝对路径导致数据库找不到

    (1).当"print os.path.dirname(__file__)"所在脚本是以完整路径被运行的, 那么将输出该脚本所在的完整路径,比如: python d:/python ...

  3. python中的os.path.dirname(__file__)的使用

    在编程时,我们要获取当前文件所在的路径,以适合所有的工程,建立相对路径. python的os.path.dirname(__file__)非常好用,建议大家使用: import os FILE = o ...

  4. 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__)返回 ...

  5. 转: Python中的os.path.dirname(__file__)

     (1).当"print os.path.dirname(__file__)"所在脚本是以完整路径被运行的, 那么将输出该脚本所在的完整路径,比如:                 ...

  6. 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 ...

  7. python中的os.path.dirname与os.path.dirname(__file__)的用法

    python中的os.path.dirname的用法 os.path.dirname(path) 语法:os.path.dirname(path) 功能:去掉文件名,返回目录 如: print(os. ...

  8. os.path.dirname(__file__)使用、Python os.path.abspath(__file__)使用

    python中的os.path.dirname(__file__)的使用 - CSDN博客https://blog.csdn.net/u011760056/article/details/469698 ...

  9. os.path.dirname(__file__)使用

    os.path.dirname(__file__)使用 该测试脚本所在的位置:D:\第1层\第2层\第3层\第4层\第5层\test11.py test11.py import os #该文件所在位置 ...

随机推荐

  1. 转载~基于比较的排序算法的最优下界为什么是O(nlogn)

    基于比较的排序算法的最优下界为什么是O(nlogn) 发表于2013/12/21 16:15:50  1024人阅读 分类: Algorithm 1.决策二叉树 回答这个问题之前我们先来玩一个猜数字的 ...

  2. [HNOI2002] 公交车路线

    题目背景 在长沙城新建的环城公路上一共有8个公交站,分别为A.B.C.D.E.F.G.H.公共汽车只能够在相邻的两个公交站之间运行,因此你从某一个公交站到另外一个公交站往往要换几次车,例如从公交站A到 ...

  3. java基础练习 16

    public class Sixtheen { /*利用递归方法求5!.*/ public static void main(String[] args){ System.out.println(&q ...

  4. kubernetes节点安装配置

    #环境安装,要与控制节点一致Centos 7 Linux release 7.3.1611网络: 互通配置主机名设置各个服务器的主机名hosts#查找kubernetes支持的docker版本Kube ...

  5. mysql 主从手动切换

    将主从(3307主--3308从)切换 前提:3307正常 一.将3307设为只读.命令行操作 # 修改配置文件 vim /etc/mysql/mysql-//my.cnf # 在[mysqld]中增 ...

  6. python tips(3);import的机制

    1.标准的import python中,所有加载到内存中的模块都是放在sys.modules中,当import一个模块的时候,会在这个列表中查看是否加载了这个模块,如果加载了,则只是将模块名字加入到正 ...

  7. PSR-2 编码风格规范

    本篇规范是 PSR-1 基本代码规范的继承与扩展. 本规范希望通过制定一系列规范化PHP代码的规则,以减少在浏览不同作者的代码时,因代码风格的不同而造成不便. 当多名程序员在多个项目中合作时,就需要一 ...

  8. 洛谷——2871[USACO07DEC]手链Charm Bracelet——01背包

    题目描述 Bessie has gone to the mall's jewelry store and spies a charm bracelet. Of course, she'd like t ...

  9. How to show only next line after the matched one?

    awk '/blah/{getline; print}' logfile

  10. 如何命令行编译Java工程

    在src下的包含Main的包下打开命令行,javac -classpath “路径到src,不到包下”  Main.java