I have a text file with floats in ascending order. I have to include a given float to this file preserving the order. I can`t work with lists, each float has to be in memory alone and see how it compares to the given number.
I tried open the file, create a auxiliary file and iterate the lines of the main file. first If the line is smaller than the given number, I write at the auxiliary file, then i write the number and I iterate again, this time writing the lines bigger than the number.
I did this code:
arq = open("teste.txt", "r")
aux = open(" aux.txt", "w")
for linha in arq:
if float(linha) < float(6.00):
aux.write(linha+"\n")
aux.write(6.00+"\n")
for linha in arq:
if float(linha) > float(6.00):
aux.write(linha+"\n")
arq.close()
aux.close()
But didn't work i expected, and i dont know why... Only the first iteration (lines smaller than number) worked and the inclusion of the number itself.
Then I close the file and open again after the first iteration, and this time everything was I expected.
arq = open("teste.txt", "r")
aux = open(" aux.txt", "w")
for linha in arq:
if float(linha) < float(6.00):
aux.write(linha+"\n")
aux.write(6.00+"\n")
arq.close()
arq = open("teste.txt", "r")
for linha in arq:
if float(linha) > float(6.00):
aux.write(linha+"\n")
arq.close()
aux.close()
I can`t figure out whats is the diference. Anyone can point me out? Look like I can iterate only once each time I open a txt file.