py2exe在我的os.listdir代码上给出错误

时间:2016-01-23 17:27:29

标签: python pygame py2exe listdir

当我从main运行我的代码时,它运行得非常好,但是当我尝试使用py2exe将main构建到exe时,它会出现此错误:

Traceback (most recent call last):


File "main.py", line 118, in <module>
    menu.menu.Menu()
  File "menu\menu.pyo", line 20, in __init__
  File "settingsManager.pyo", line 61, in getSetting
  File "settingsManager.pyo", line 148, in __init__
WindowsError: [Error 3] The system cannot find the path specified: 'C:\\Users\\digiholic\\git\\universalSmashSystem\\main.exe\\settings\\rules/*.*'

它指的是:

for f in os.listdir(os.path.join(os.path.dirname(__file__),'settings','rules')):

看起来os.listdir正在使用unix文件路径来查找每个文件,而Windows却没有。有没有办法以一种不会炸毁一切的方式使用listdir?

1 个答案:

答案 0 :(得分:2)

当你在exe中运行时,你需要检查模块是否为frozen__file__的路径通常不是你在exe和原始python中的期望脚本。您需要使用以下内容访问该位置:

import imp, os, sys

def main_is_frozen():
   return (hasattr(sys, "frozen") or # new py2exe
           hasattr(sys, "importers") # old py2exe
           or imp.is_frozen("__main__")) # tools/freeze

def get_main_dir():
   if main_is_frozen():
       return os.path.dirname(sys.executable)
   return os.path.dirname(sys.argv[0])

来源:http://www.py2exe.org/index.cgi/HowToDetermineIfRunningFromExe 您还可以在此处查看其他方向:http://www.py2exe.org/index.cgi/WhereAmI