Executing a function when the console window closes?
<p>I want to execute a function when the program is closed by user.</p>

<p>For example, if the main program is <code>time.sleep(1000)</code>,how can I write a txt to record unexpected termination of the program.</p>

<p>The program is packaged into exe by cxfreeze. Click the "X" to close the console window.</p>

<p>I know <a href="http://docs.python.org/3.3/library/atexit.html" rel="nofollow">atexit</a> can deal with sys.exit(),but is there a more powerful way can deal with close window event?</p>

<p><strong>Questions</strong></p>

<ol>
<li>Is this possible in Python?</li>
<li>If so, how can I do this?</li>
</ol>
