0

When stoping the script with ctrl+alt+m python wont catch it as an interruption. I want to execute a cleanup script before exiting.

if __name__ == "__main__":
    try:
        main()

    except KeyboardInterrupt:
        print("keyboardInterrupt")

    finally:
        handle_cleanup()

How does code runner stop codes and what are my options to achive my goal?

Lambda
  • 23
  • 3

1 Answers1

0

I think you could just use the atexit module:

import atexit

def main ():
  atexit.register(handle_cleanup)
  ...

if __name__ == "__main__":
  main()
Gabriel Milan
  • 702
  • 2
  • 7
  • 21
  • I tried what you said and it did not work with code runner but it did work with "vanilla" terminal. – Lambda Jul 15 '20 at 14:34
  • You're right. Haven't tried testing on VSCode Code Runner. May I ask wheter VSCode Code Runner is really necessary? – Gabriel Milan Jul 15 '20 at 14:40
  • It's actually not, but I prefer how clean it is. Does not show directroy and other stuff like that which a terminal would do. – Lambda Jul 15 '20 at 14:41
  • If that's what you want, you could just `export PS1='> '` on terminal. That way, the prompt would be just `>`. – Gabriel Milan Jul 15 '20 at 14:44
  • It seems like doing this kind of action using VSCode Code Runner [is not possible using the exit announcement from process](https://github.com/microsoft/vscode/issues/76284#issuecomment-506662963). The link I've attached has a Node workaround, but I don't know if Python has a similar one. – Gabriel Milan Jul 15 '20 at 14:52