Python 解析时出现意外的 EOF

发布于 2024-11-25 13:33:15 字数 656 浏览 1 评论 0原文

我的代码:

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

My code:

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))

The error:

  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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

宫墨修音 2024-12-02 13:33:15

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

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

另外:请确保您了解使用的危险评估

This could happen if the hist_file exists but is empty

You should print hist_data before you try to eval it so you can see for sure

Also: Make sure you understand the dangers of using eval

话少心凉 2024-12-02 13:33:15

正如 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”对您的项目/工作有哪些缺陷和/或可能的缺点。

As gnibbler has explained, this can occur if a file (in this case "history.dat") already exists but is empty.

Test ran it in python v3 IDLE using:

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()

Ran perfectly on the first go; however when i manually removed the lines and made the file empty, it gave the same error you are getting. Again, as gnibbler pointed out, first see what flaws and/or possible down points "eval" has to your project/work..

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文