django os.path.dirname(__ file__) [英] django os.path.dirname(__file__)

查看:252
本文介绍了django os.path.dirname(__ file__)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从书中做练习:
http://www.tangowithdjango .com / book17 / chapters / templates_static.html

I am doing exercises from book: http://www.tangowithdjango.com/book17/chapters/templates_static.html

我有这个代码的问题:

import os

print __file__
print os.path.dirname(__file__)
print os.path.dirname(os.path.dirname(__file__))

它打印打印目录名称。而不是打印第一行(文件名)
,但第二行和第三行也打印但是空。

It shold print dir names. Instead of that it prints first line ( filename) but 2nd and 3rd line are also printed but empty.

我在Windows 7上使用Python 2.7获得此行为和Ubuntu 14.04

I get this behavior with Python 2.7 on Windows 7 and Ubuntu 14.04

编辑:使用这段代码,我得到的绝对路径与 os.path.dirname(__ file __)

with this code i get absolute path with os.path.dirname(__file__):

import os
import django
import settings

print settings.BASE_DIR

如果相同的代码从settings.py打印并直接编码,有什么区别? >

What is the difference if the same code is imporeted from settings.py and coded directly?

推荐答案

这是程序所在的目录的绝对路径哪个是你想要的我相信。 / p>

This is the absolute path of the directory where the program resides which is what you want I believe.

os.path.abspath(os.path.dirname(__file__))

这是程序的父目录

os.path.join(os.path.dirname(__file__), 'some_directory')

这是缩写直接程序所在的ory

This is the abbreviated directory where the program resides

os.path.dirname(os.path.realpath(__file__))

这篇关于django os.path.dirname(__ file__)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆