![](/img/trans.png)
[英]Python - Error - AttributeError: '_io.TextIOWrapper' object has no attribute 'insert'
[英]Python - Attribute Error '_io.TextIOWrapper' object has no attribute 'open'
我收到一个错误
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])
它应该是
File=open(classname+'.txt','a')
File.write(name+','+surname+','+str(1)+','+str(score)+'/n')
File.close()
问题是您一开始就声明
File=open(classname+'.txt','r+')
然后您再次要求打开文件
File.open(classname+'.txt','a')
但File
已open(classname+'.txt','r+')
。 只需跳过File.open(classname+'.txt','a')
,它就可以正常工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.