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

时间:2022-07-08 12:05:37

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.

我正在使用os模块在Django项目设置中具有相对路径。py文件。变量SITE_ROOT被设置为设置的当前工作目录。然后使用py文件引用同样位于同一目录中的所有静态/媒体目录。

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__))

在设置。py,上面的语句都有相同的输出。但是我的模板只有在使用SITE_ROOT = os.path.abspath(os.path.dirname(__file__))时才会加载)

Django looks for the templates here:

Django在这里寻找模板:

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

将SITE_ROOT设置为os.getcwd()似乎使Django在设置上面的目录中查找模板文件夹。py文件

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

我可以很容易地不使用os.getcwd()和我的站点运行良好,但是我很好奇这里会发生什么:

Anyone know?

有人知道吗?

4 个解决方案

#1


36  

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

正如mouad所说,os.getcwd()不会给出您所期望的结果。

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.

getwd cwd()不仅仅返回当前工作目录。它默认为您的env中的$PWD。它不是脚本所在的位置,而是执行脚本时的位置。

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

在/home/user中进行python管理。py, os.getcwd()将返回/home/user,并执行python用户/管理。py, os.getcwd()将返回/回家

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().

但是,由于可以使用os.chdir(),所以它仍然不总是正确的。它也会改变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.

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

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.

正如肖恩福莫所说,__file__可能并不总是绝对的。为了更好地了解它是如何工作的,您可以检查这个答案:Python __file__属性。而且,正如Python3.4一样,__file__应该始终是一条绝对路径。

Hope it's clear.

希望很明显。

#2


4  

The command os.path.abspath(os.path.dirname(__file__)) returns the directory in which the code file is stored, but os.getcwd() gives you your current working directory which is by default where the code was executed, the latter can be changed using the os.chdir() command.

命令os.path.abspath(os.path.dirname(__file__))返回存储代码文件的目录,但是os.getcwd()提供当前工作目录,默认情况下执行代码的位置是当前工作目录,后者可以使用os.chdir()命令进行更改。

#3


2  

os.getcwd() will not give you the path where the settings.py is located rather it will give you the path from where the script (in your case manage.py) is executed.

os. getwd cwd()不会给您设置的路径。py位于适当的位置,它将为您提供执行脚本(在案例管理.py中)的路径。

#4


0  

If the two statements you show truly have the same output, then either should work. So either: 1) they are subtly different, for example, one has a trailing slash, the other doesn't, or 2) you are testing in one environment and running in another.

如果您显示的这两个语句的输出确实相同,那么这两个语句都应该有效。因此,要么是:1)它们有微妙的不同,例如,一个有斜杠,另一个没有,或者2)你在一个环境中测试,在另一个环境中运行。

#1


36  

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

正如mouad所说,os.getcwd()不会给出您所期望的结果。

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.

getwd cwd()不仅仅返回当前工作目录。它默认为您的env中的$PWD。它不是脚本所在的位置,而是执行脚本时的位置。

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

在/home/user中进行python管理。py, os.getcwd()将返回/home/user,并执行python用户/管理。py, os.getcwd()将返回/回家

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().

但是,由于可以使用os.chdir(),所以它仍然不总是正确的。它也会改变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.

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

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.

正如肖恩福莫所说,__file__可能并不总是绝对的。为了更好地了解它是如何工作的,您可以检查这个答案:Python __file__属性。而且,正如Python3.4一样,__file__应该始终是一条绝对路径。

Hope it's clear.

希望很明显。

#2


4  

The command os.path.abspath(os.path.dirname(__file__)) returns the directory in which the code file is stored, but os.getcwd() gives you your current working directory which is by default where the code was executed, the latter can be changed using the os.chdir() command.

命令os.path.abspath(os.path.dirname(__file__))返回存储代码文件的目录,但是os.getcwd()提供当前工作目录,默认情况下执行代码的位置是当前工作目录,后者可以使用os.chdir()命令进行更改。

#3


2  

os.getcwd() will not give you the path where the settings.py is located rather it will give you the path from where the script (in your case manage.py) is executed.

os. getwd cwd()不会给您设置的路径。py位于适当的位置,它将为您提供执行脚本(在案例管理.py中)的路径。

#4


0  

If the two statements you show truly have the same output, then either should work. So either: 1) they are subtly different, for example, one has a trailing slash, the other doesn't, or 2) you are testing in one environment and running in another.

如果您显示的这两个语句的输出确实相同,那么这两个语句都应该有效。因此,要么是:1)它们有微妙的不同,例如,一个有斜杠,另一个没有,或者2)你在一个环境中测试,在另一个环境中运行。