Ismasou
Ismasou

Reputation: 135

How to make python write on a file live

Hello my question is simple, I have a loop that write results on a file and it takes time to go through the whole loop, is there a way I can make python write every result it gets, or do I have to close the file inside the loop ? will it be longer ? thank you guys

fi= open('results','a') 
for j in range(1,100):
    tt=j
    res=scipy.optimize.newton(zero,15)
    fi.write("\n"+str(tt)+", " + str(res)+", "+str( zero(res) ) )
print("done")
print("--- %s seconds ---" % (time.time() - start_time))
winsound.Beep(300,2000)
fi.closed

Upvotes: 0

Views: 2063

Answers (1)

Kevin Lemaire
Kevin Lemaire

Reputation: 997

You should be able to write using the flush() function.

outputFile.flush()

More informations on the flush() function on this link: https://docs.python.org/2/library/functions.html#print

That's nearly the same request than this one: Python 2.7 : Write to file instantly

Upvotes: 1

Related Questions