【问题标题】:Python unexpected EOF while parsing解析时Python意外的EOF
【发布时间】:2011-07-22 15:04:34
【问题描述】:

我的代码:

def getAppHistory(self):
    path = self.APP_STORAGE + "\\history.dat"
    if os.path.exists(path):
        hist_file = open(path, "r")
        hist_data = hist_file.read()
    else:
        hist_file = open(path, "w")
        hist_data = "[200, \"Empty\", \"You have no device history!\", \"self.Void\"]"
        hist_file.write(hist_data)
    self.conn_menu.append(eval(hist_data))

错误:

  File "C:\Users\Judge\Desktop\Lulz\Lulz.py", line 113, in getAppHistory
    self.conn_menu.append(eval(hist_data))
  File "<string>", line 0

   ^
SyntaxError: unexpected EOF while parsing

【问题讨论】:

    标签: python eof


    【解决方案1】:

    如果hist_file 存在但为空,则可能发生这种情况

    您应该在尝试评估它之前打印hist_data,以便您可以确定

    另外:确保您了解使用 eval 的危险

    【讨论】:

      【解决方案2】:

      正如 gnibbler 所解释的,如果文件(在本例中为“history.dat”)已经存在但为空,则可能会发生这种情况。

      测试在 python v3 IDLE 中使用:

      import os
      def getAppHistory():
          path = "C:/test/history.dat"
          if os.path.exists(path):
              hist_file = open(path, "r")
              hist_data = hist_file.read()
          else:
              hist_file = open(path, "w")
              hist_data = "[200, \"Empty\", \"You have no device history!\", \"self.Void\"]"
              hist_file.write(hist_data)
          print(eval(hist_data))
          hist_file.close()
      

      第一次跑得很好;但是,当我手动删除这些行并使文件为空时,它给出了同样的错误。同样,正如 gnibbler 指出的那样,首先查看“eval”对您的项目/工作有哪些缺陷和/或可能的缺点。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-03-21
        • 2015-12-08
        • 1970-01-01
        • 1970-01-01
        • 2011-07-15
        • 1970-01-01
        • 1970-01-01
        • 2017-10-03
        相关资源
        最近更新 更多