1.获取当前路径
curPath = os.path.abspath(os.path.dirname(__file__))
2.获取项目根路径
# InfoServiceSystem是项目名
rootPath = curPath[:curPath.find("InfoServiceSystem\\")+len("InfoServiceSystem\\")]
3.获取任意文件路径
# 获取static文件夹下stopwords.txt文件路径
dataPath = os.path.abspath(rootPath + 'static\\stopwords.txt')
4.简单应用
1.根目录下新建rootpath.py
import os
rootPath = os.path.abspath(os.path.dirname(__file__))
2.某子项目目录下需要用到另一子目录下的文件
from rootpath import rootPath
dataPath = os.path.abspath(rootPath + '\\static\\stopwords.txt')
【注意这里子目录前也要加\\】