2015-06-09 807 views
1

我收到一個錯誤的Python - 屬性錯誤「_io.TextIOWrapper」對象有沒有屬性「打開」

File.open(classname+'.txt','a') 
AttributeError: '_io.TextIOWrapper' object has no attribute 'open' 

試圖打開一個文件。我需要打開文件並用分數寫入文件。

下面是代碼

if Exists==False: 
    File.open(classname+'.txt','a') 
    File.write(name+','+surname+','+str(1)+','+str(score)+'/n') 

else: 
    File=open(classname+'.txt','w') 
    linecount=len(filelines) 
    for i in range(0,linecount): 
     File.write(filelines[i]) 
+3

您說的行導致錯誤未出現在您發佈的代碼中。 –

+1

給出完整的錯誤,包括行號,並指出它發生的位置。 –

+0

道歉@丹尼爾,我編輯了屬性錯誤,它應該是現在它是什麼。 – UserIsMe

回答

3

應該

File=open(classname+'.txt','a') 
File.write(name+','+surname+','+str(1)+','+str(score)+'/n') 
File.close() 
0

的問題是,在開始的時候你聲明

File=open(classname+'.txt','r+') 

,然後你再問打開文件

File.open(classname+'.txt','a') 

但是File已經是open(classname+'.txt','r+')。只需跳過File.open(classname+'.txt','a'),它應該可以正常工作。

相關問題