Python - AttributeError: '_io.TextIOWrapper' object has no attribute 'append'
You got your append code all mixed up; the append()
method is on the filelines
object:
ClassFile=open(CN+'.txt','r')
line=ClassFile.readline()
while line!='':
filelines.append(line)
ClassFile.close()
Note that I also moved the close()
call out of the loop.
You don't need to use a while
loop there; if you want a list with all the lines, you can simply do:
ClassFile=open(CN+'.txt','r')
filelines = list(ClassFile)
ClassFile.close()
To handle file closing, use the file object as a context manager:
with open(CN + '.txt', 'r') as openfile:
filelines = list(openfile)
ClassFile
is an object of type _io.TextIOWrapper
which does not has any attribute append
. You are mistaking it to be an object of type List. It seems in place of ClassFile.append(filelines) you want something like filelines.append(line)
.
If you want to write something into a file, open it in write or append mode (depending on your need) and write into it the string you want.