python 中怎么获取工作路径

2025-04-15 17:25:23
推荐回答(1个)
回答1:

import os

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

可以参考django的方式。注意,若获取路径的文件位于项目根目录中,则需要剥掉一层os.path.dirname :

import os

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