One last function before application closes? (Python) : Ok so look, I'm getting desperate since I've already been all over the internet trying to find the answer to this question. so sorry if this has been answered somewhere else, I couldn't find it just give me the link.


So basically I have a python file that is constantly using the ```f.write()``` function to write to another text file, however, the script doesn't stop writing to the file until it's closed manually.

This is all fine and dandy, except for the fact that any delay greater than 0.01 doesn't seem to save my edits to the text file, so doing a little bit of investigation, an easy fix is the ```f.close()``` function.

So what I need is a line of code that can detect once the script has been stopped, and put out one last function before being fully closed.