1
I have a job to do but I can’t find the error. This excerpt from the program needs to add words in a text file cumulatively. That is, every time you go through the loop will add numbers and words in the text file without deleting what is already written.
What I’ve done so far is like this:
arqParcial=open("parcial.txt","r")
texto=arqParcial.readline()
f1=[]
for i in range (len(texto)):
palavra=texto[i].split()
f1.append(palavra)
print(f1)
arqParcial.writelines(f1)
arqParcial.close()
But you keep making that mistake:
Traceback (most recent call last):
File "C:/Python34/teste3.py", line 8, in <module>
arqParcial.writelines(f1)
TypeError: must be str, not list
I have no idea why.
The program is much longer. I just put in this excerpt to see if you can tell me why the mistake.
Got it. But then I can’t read what’s in the file, can I? It’s giving this error now of not readable. Or I can with the 'a' write directly that won’t erase what’s there?
– Naiane Negri
Exactly. Append (or simply "a") writes everything at the end of the file. I updated the answer.
– Leonel Sanches da Silva
Got it here! Thanks a lot! By the way, it works to add an array too?
– Naiane Negri
It does work. See here how the flow of a question and answer site works. Thank you!
– Leonel Sanches da Silva