[Python] How to detect when user closes a console application via "X" button

Discussão em 'Python' iniciado por Stack, Março 15, 2025.

  1. Stack

    Stack Membro Participativo

    I have a console-based Python program running under Windows. The program maintains most of its data in memory and periodically saves the data to disk, or when the user shuts the application down via a keyboard interrupt (Ctrl + C) event.

    The problem I have is that when a user hits the "X" button at the top right of the console window, the session closes and the data in memory is lost. What I am looking for is an event/signal or hook so that I can clean up the memory before closing down.

    I hope to do this without any external libraries, though if this is not possible I'd still like to know how it can be done.

    Continue reading...

Compartilhe esta Página