0

This might sound like a really dulled down question but I have honestly searched everywhere for it but is there a way where once the user clicks the "exit" or "stop" button to stop there program right after you click that it will write data to a file somewhere? or would that be impossible since you closesd that program? I honestly don't know, Here's my try at it Its nothing really because I don't entirely know how to do it, but I just say this

if (onExit):
    f = open('file.txt', mode='w')
    f.write (data)
    f.close

my onExit is just a Boolean and yeah I'm just not sure how to do it, I know how dumb that code looks btw I just didn't know how to show to you guys that I have tried looking for it other then if I showed you my history tab

cunniemm
  • 679
  • 5
  • 19

2 Answers2

1

Clicking an 'exit' button typically does not actually close a program immediately. Instead, the code that runs when that button is pushed also takes care of saving data.

If we are talking about a console application, which is 'closed' by ctrl-c (i.e. a KeyboardInterrupt), you can use a try-except block:

try:
    raw_input()
except KeyboardInterrupt:
    # save here
    raise

Python does support atexit handlers, but they are most likely not the right solution to your problem.

valhallasw
  • 341
  • 1
  • 5
  • I am using eclispe, I just want it so if I click the stop button then it automaticly saves my data to a text file if that is possible – cunniemm Jul 28 '15 at 16:55
0

If you're using PyDev on Eclipse, the terminate button (red square) sends a kill message to the system, which in turn will kill your program without executing further code.

As the previous answer says, you can use the atexit module, but that only works when your program ends normally.

See also: Is it possible for Eclipse to terminate gently instead of using SIGKILL?

Community
  • 1
  • 1
Kong Huang
  • 11
  • 2