Python - Attribute Error '_io.TextIOWrapper' object has no attribute 'open'

廉价感情. 提交于 2019-12-11 02:58:38

问题


I receive an error

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

while trying to open a file. I need to open the file and write to the file with the scores.

Here is the code

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])

回答1:


it should be

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



回答2:


The problem is that at the beginning you declare

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

and then you ask again to open File

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

but File is already open(classname+'.txt','r+'). Just skip File.open(classname+'.txt','a') and it should work fine.



来源:https://stackoverflow.com/questions/30728990/python-attribute-error-io-textiowrapper-object-has-no-attribute-open

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!