思路: 通过确定可执行文件的目录,从而确定相对目录/相对路径
具体是使用sys.frozen判断目前是否是直接执行,使用sys.executable获取可执行文件路径。
代码:
import os
import sys
if getattr(sys, 'frozen', False):
root_path = os.path.dirname(sys.executable)
elif __file__:
root_path = os.path.dirname(__file__)
root_dir即所需目录。