2015-05-29 43 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)的文件有複雜性,所以我剛剛刪除了很多東西,並開始新鮮。

相關問題