python获取当前项目根路径
两行代码搞定
import os class BaseUtil: # 获取项目根路径 def get_item_path(self, item_name): """ :param item_name: 项目名称,如pythonProject :return: """ # 获取当前所在文件的路径 cur_path = os.path.abspath(os.path.dirname(__file__)) # 获取根目录 return cur_path[:cur_path.find(item_name)] + item_name
>>>执行结果
C:\Users\86158\Desktop\pythonProject