网上的方法很多,我分享一下我常用的方法,核心代码非常简单:
import os
import sys
def getRootPath():
curPath = os.path.abspath(os.path.dirname(__file__))
rootPath = curPath[:curPath.find("项目名\\")+len("项目名\\")]
return rootPath
def getOtherPath(abspath):
rootPath = getRootPath()
dataPath = os.path.abspath(rootPath + abspath);
return dataPath
def resource_path(relative_path):
if getattr(sys, 'frozen', False):
base_path = sys._MEIPASS
print('true',base_path)
else:
base_path = os.path.abspath(".")
print('false ',base_path)
print(os.path.join(base_path, relative_path))
return os.path.join(base_path, relative_path)
|