os.getcwd()vs os.path.abspath(os.path.dirname(__ file__)) [英] os.getcwd() vs os.path.abspath(os.path.dirname(__file__))

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

问题描述

我正在使用os模块在我的Django项目中具有相对路径 settings.py 文件。变量SITE_ROOT设置为 settings.py 文件的当前工作目录,然后用于引用所有 static / media 目录也位于同一目录中。



我的问题:

  print os.getcwd()
print os.path.abspath(os.path.dirname(__ file__))

在设置.py,上述语句都有相同的输出。但是如果我使用 SITE_ROOT = os.path.abspath(os.path.dirname(__ file __))


$ b $,我的模板将只会加载b

Django在这里找到模板:

  TEMPLATE_DIRS =(
os.path.join(SITE_ROOT, 'template'),

SITE_ROOT设置为 os。 getcwd()似乎使Django在目录中找到模板文件夹ABOVE settings.py 文件



我可以很容易地使用 os.getcwd(),我的网站运行正常,但我很好奇这里可能会发生什么:)



任何人都知道?

解决方案

正如mouad所言, os.getcwd()不会给你准确的期望。



os.getcwd()比返回当前工作目录有点多。在你的环境中默认为$ PWD。脚本不在哪里,而执行脚本的地方不在此。



正在 / home / user 并且执行 python manage.py os.getcwd()将返回 / home /用户
/ home / 中,并执行 python user / manage.py os.getcwd()将返回 / home



但是它仍然不会一直是真的,因为可以使用 os.chdir()。就像在 cd 一样。它也会更改 os.getcwd()的返回值。另一方面,



__ file __ 是模块文件的路径。所以你必须使用这个来确定一个相对于你的模块的路径,而不是当前可能改变的工作目录。



正如ShawnFumo所说, __ file __ 可能并不总是绝对的。为了更好地了解它的工作原理,您可以检查答案: Python __文件__ 属性。另外,从Python3.4开始, __ file __ 应该是绝对路径。



希望很清楚。 >

I am using the os module to have relative paths in my Django projects settings.py file. The variable SITE_ROOT is set to the current working directory of the settings.py file and then used to reference all of the static/media directories also located in that same directory.

Heres my issue:

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

In settings.py, the above statements both have identical outputs. but my template will only load if I use SITE_ROOT = os.path.abspath(os.path.dirname(__file__))

Django looks for the templates here:

TEMPLATE_DIRS = (
    os.path.join(SITE_ROOT, 'templates'),
)

SITE_ROOT set to os.getcwd() seems to make Django look for the templates folder in the directory ABOVE the settings.py file

I can just as easily not use os.getcwd() and my site runs fine, but I am curious what may be going on here :)

Anyone know?

解决方案

As mouad said, os.getcwd() won't give you exactly what you're expecting.

os.getcwd() does a bit more than returning the current working directory. It default to $PWD in your env. It's not where the script is located but where you were when you executed the script.

Being in /home/user and doing python manage.py, os.getcwd() will return /home/user Being in /home/ and doing python user/manage.py, os.getcwd() will return /home

But it's still won't be always true since it's possible to use os.chdir(). It is in other word like doing cd. It will also change the return value of os.getcwd().

On the other hand. __file__ is the path of the module's file. So you have to use this to be certain to have a path relative to your module instead of the current working directory that may change.

As ShawnFumo said, __file__ might not be always absolute. To get a better idea on how it works, you can check that answer: Python __file__ attribute. Also, as of Python3.4 __file__ should always be an absolute path.

Hope it's clear.

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

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