The meaning and usage of os.path.abspath() and os.path.dirname() in the django configuration file

1. The
basic configuration in the project structure django configuration file setting.py:

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

Second, print the results
We can see the results obtained by printing:

# Build paths inside the project like this: os.path.join(BASE_DIR, ...)
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
print(os.path.abspath(__file__))
print(os.path.dirname(os.path.abspath(__file__)))
print(BASE_DIR)
print(os.path.basename(__file__))

Insert picture description here

Three, function usage introduction:
1. os.path.abspath() :

os.path.abspath(__file__) 功能: 获取当前脚本的完整路径

Note: os.path.abspath( file ) returns the absolute path of the script, that is, the absolute path of the setting.py file.
2. os.path.dirname() :

os.path.dirname(path)  功能:去掉文件名,返回目录

Note: os.path.dirname(path) returns the parent path of path; it can be nested, os.path.dirname(os.path.dirname(path)) returns the parent path of the parent path.
3. os.path.basename( file ) :

os.path.basename(__file__)  功能:返回脚本的文件名称

Guess you like

Origin blog.csdn.net/qq_34663267/article/details/109289073