开发者

Python 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: unexpecte开发者_高级运维d EOF while parsing


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


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜