2015-05-29 45 views
2

我不知道为什么我收到一个错误消息,当我运行的addItem()说:我不断收到这个错误,当我运行我的功能: - 语法错误:意外的EOF在解析

SyntaxError: unexpected EOF while parsing 

我有我所有的阅读和写作文件都关闭了,我正在使用raw_input函数。我认为我的readfrom()函数有问题,但我不知道究竟是什么。

import ast 


def writeTo(): 

    itemx=",".join(map(str,itemx)) 
    with open("ListItemz.txt","a") as filewrite: 
     filewrite.write(str(itemx)+"\n") 
    filewrite.close() 

def readfrom(): 

    fileread=open("ListItemz.txt","r") 
    fr=fileread.readlines() 
    fr=fr[len(fr)-1] 
    itemx=list(ast.literal_eval(fr)) 
    fileread.close() 

itemx=[] 

def addItem(): 

    global itemx 
    if itemx==[]: 
     itemx=[] 
    else: 
     """ 
     about to read file: 
     """ 
    readfrom() 
    dowhile="y" 
    while dowhile =="y": 
     item=raw_input("please enter item discription, or barcode No. ") 
     if itemx!=[]: 
      for y in itemx: 
       if item==y[0] or item==y[1]: 
        raise TypeError("This Item is already added") 
     case=int(raw_input("Enter how much holds in one (1) case: ")) 
     caseNo=int(raw_input("how many cases are there? ")) 
     for i in stockList(): 
      if item==i[1] or item==i[0]: 
       itemx+=[[i[0],i[1],case,caseNo]] 
       print "ITEM ADDED" 
     dowhile=raw_input("Do you want to add another?(Y/N) ") 
     """ 
     about to write itemx to a file: 
     """ 
     writeTo() 
    return itemx 
+0

'fr = fr [len(fr)-1]'的价值是什么?像ast.literal_eval(“(1,”)''可以产生相同的错误,发布完整的错误追溯。 –

+0

我没有得到这样的错误发布在这里的代码。EOF错误意味着Python期待的东西*其他*但是你没有提供它,这是*整个*文件吗? –

+0

你不需要'关闭()'已经被'with'构造关闭的文件。 – TigerhawkT3

回答

1

我不认为错误来自上面的代码。解析和语法指的是试图读取你的程序的Python解析器,而不是你读的或写的。由于上面的程序不完整 - 没有主程序 - 很难看到错误的起源。或者可能有的一个主程序,但缩进是关闭的。

还有这个奇怪的构造:

if itemx==[]: 
    itemx=[] 

那真的是正确的吗?

要尝试找出问题,可以使用(低估的)Python调试器(pdb)。添加import pdb在顶部,并插入要停止程序的一行:

pdb.set_trace() 

那么你可以这样做:

n<enter> to advance a line of code 
p variable<enter> to see the value of a variable 
c<enter> to continue at full speed 
... and a lot more - see the pdb manual. 

通过该计划,直到错误啪啪只是前进。

2

我写给(ListItemz.txt)的文件有复杂性,所以我刚刚删除了很多东西,并开始新鲜。

相关问题