Python 解析时出现意外的 EOF
我的代码:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果
hist_file
存在但为空,则可能会发生这种情况您应该在尝试评估它之前打印
hist_data
,以便您可以确定另外:请确保您了解使用的危险
评估
This could happen if the
hist_file
exists but is emptyYou should print
hist_data
before you try to eval it so you can see for sureAlso: Make sure you understand the dangers of using
eval
正如 gnibbler 所解释的,如果文件(在本例中为“history.dat”)已经存在但为空,则可能会发生这种情况。
测试在 python v3 IDLE 中运行它,使用:
第一次就完美运行;但是,当我手动删除这些行并使文件为空时,它给出了与您得到的相同的错误。再次,正如 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:
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..