English 中文(简体)
os.path.dirname (os.path.abspath (__file__) and os.path.dirname (__file__)
原标题:Difference between os.path.dirname(os.path.abspath(__file__)) and os.path.dirname(__file__)

“Django”项目文档中包含以下两条内容:

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
PROJECT_ROOT = os.path.dirname(os.path.abspath(__file__))

我想知道这一区别,因为我认为两者都提到同一目录。 如果你能够提供某些链接,也会大有助益。

最佳回答

。 你们可以重新制定两项定义:

PROJECT_ROOT = os.path.dirname(os.path.abspath(__file__))
BASE_DIR = os.path.dirname(PROJECT_ROOT)

由于,os.path.dirname(Function简单地排除了一条道路的最后一段。

见上文<代码>_file_。 现有模块的名称,见

http://www.un.org/Depts/DGACM/index_french.htm

然而,它可以是relative path,这样,>=。 在删除档案名称和储存模块在<代码>上的全行程之前,将这一条变成一条绝对的道路。 PROJECT_ROOT。

问题回答

附录一

my_new_app
|
└───my_new_app
│   │   
│   │   settings.py
│   │   ...
|   manage.py

然后,os.path.abspath(__file__) 回到绝对的道路上来dings.py file,os.path.dirname(os.path.abspath(__file__)) 返回内联网的路程my_new_app rafter and path.dirname(os.path.dirname (os.path.spth.abath(file__file__))code/code 返回外联网

在最近版本的Django(例如,第4.2.5版)中,而不是os ,pathlib。 模块使用,因此对应结构是:

from pathlib import Path

BASE_DIR = Path(__file__).resolve().parent.parent
PROJECT_ROOT = Path(__file__).resolve().parent

There s a special case where if settings.py is already in the root directory on your environment (e.g. the C drive on a local machine) so that its path looks like C:settings.py, then BASE_DIR == PROJECT_ROOT may be True. This is probably not how it should be, so if BASE_DIR and PROJECT_ROOT are the same, then you probably need to re-structure your project.





相关问题
Can Django models use MySQL functions?

Is there a way to force Django models to pass a field to a MySQL function every time the model data is read or loaded? To clarify what I mean in SQL, I want the Django model to produce something like ...

An enterprise scheduler for python (like quartz)

I am looking for an enterprise tasks scheduler for python, like quartz is for Java. Requirements: Persistent: if the process restarts or the machine restarts, then all the jobs must stay there and ...

How to remove unique, then duplicate dictionaries in a list?

Given the following list that contains some duplicate and some unique dictionaries, what is the best method to remove unique dictionaries first, then reduce the duplicate dictionaries to single ...

What is suggested seed value to use with random.seed()?

Simple enough question: I m using python random module to generate random integers. I want to know what is the suggested value to use with the random.seed() function? Currently I am letting this ...

How can I make the PyDev editor selectively ignore errors?

I m using PyDev under Eclipse to write some Jython code. I ve got numerous instances where I need to do something like this: import com.work.project.component.client.Interface.ISubInterface as ...

How do I profile `paster serve` s startup time?

Python s paster serve app.ini is taking longer than I would like to be ready for the first request. I know how to profile requests with middleware, but how do I profile the initialization time? I ...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

Converting Dictionary to List? [duplicate]

I m trying to convert a Python dictionary into a Python list, in order to perform some calculations. #My dictionary dict = {} dict[ Capital ]="London" dict[ Food ]="Fish&Chips" dict[ 2012 ]="...