Write to File after Program Closes - python

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

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.

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?

Related

How do I prevent ^C from showing when displayed when pressing CTRL+C

I am trying to prevent ^C from showing when a user presses CTRL+C while my script is running.
Why do I want to prevent that?
Because things like this will happen and it does not look nice:
$ python3 myscript.py
^CYour pressed CTRL+C
I know there is a similar question here, but it does not work in Python
I found an easy method!
# Returning the cursor to home and dont create a new line
print("\r", end="")
# Now we are able to print on the line where ^C would be displayed
print("Your pressed CTRL+C")
Because things like this will happen...
Just my two cents: the fact that ^C displays in the terminal is a good thing. It's confirmation that SIGINT was sent to the process, as expected. Don't try to remove it; instead, as others have suggested, start a new line if you really want to. Or just exit without printing anything additional at all, like lots of other command line applications.

KUbuntu, Python and libtcod - console_is_window_closed not working...?

I've done a reasonable amount of coding with the libtcod library, both the C# and python wrappers. My current setup is KUbuntu 14.10, python 2.7.8, and libtcod 1.5.2.
I've made a few programs that work fine, but the latest I've just started doesn't seem to want to allow me to close the console window.
I can send a CTRL+C from the console that I run the program from, and it will close, but, no amount of clicking on the window's "x" button, or Alt+F4s seem to work.
My code is as follows:
'''
justclose.py
'''
import sys
import time
import libtcodpy as libtcod
libtcod.console_set_custom_font(b'lucida12x12_gs_tc.png', libtcod.FONT_TYPE_GREYSCALE | libtcod.FONT_LAYOUT_TCOD)
libtcod.console_init_root(50,50, "The ever-present window", False)
libtcod.console_flush()
while not libtcod.console_is_window_closed():
time.sleep(1)
sys.exit
When I run the program, the console comes up, as expected, and sits around waiting for console_is_window_closed to return true, which it never does. I'm not sure where the problem lies. I can run other programs that use the same initialisation code, and same while loop and which respond just fine to me clicking the close button on the console window.
I've tried looking through an strace of the process, but, I'm not sure I'm up to the task of deciphering it. Nothing looked immediately out of the ordinary.
I'd like some advice on how to track down what's going wrong. Thanks.
EDIT: specifically, I'd like to know how I can check that the close window event is propagating at all, and if so, how far, where it's getting trapped/ignored, that sort of thing. When I run through strace, I see absolutely nothing happening when I click the close button. Is there some better way to debug this?
Replace time.sleep(1) with libtcod.console_check_for_keypress(). When the program sleeps 1 millisecond for each iteration, the program can not respond when you press X. It exits when you press CTRL+C because the program receives the SIGINT signal and it exits immediately. Replacing time.sleep(1) with libtcod.console_check_for_keypress() makes the program check the key pressed on the keyboard, if there is one. That way, the program doesn't block the execution.

Waiting for a program to finish its task

I'd like to know how to have a program wait for another program to finish a task. I'm not sure what I'd look for for that...
Also, I'm using a mac.
I'd like to use Python or perhaps even applescript (I could just osascript python if the solution if for applescript anyway)
Basically this program "MPEGstreamclip" converts videos, and it opens what appears to be 2 new windows while it's converting. One window is a conversion progress bar, and the other window is a preview of the conversion. (Not sure if these actually count as windows)
(Also, MPEGstreamclip does not have an applescript dictionary, so as far as I know, it can't listen for certain window names existence)
But basically I want my program to listen for when MPEGstreamclip is done, and then run its tasks.
If it helps, when the conversion is done, the mpegstreamclip icon in the dock bounces once. I'm not sure what that means but I'd think you could use that to trigger something couldn't you?
Thanks!
I realized GUI applescript was the answer in this scenario. With it I could tell the PROCESS to get every window, and that worked. However, I'm leaving this up because I'd like to know other ways. I'm sure this GUI workaround won't work for everything.
If the MPEGstreamclip actually ends when it is done, you could wrap the whole thing up in a python script using various techniques already discussed in another post. Just be sure to wait for the external process to end before continuing with your other steps.

How to prevent termination of a running program using "ctrl+c" in Linux using python?

I have written a piece of code in python, in which I am asking questions and users should give their input. Sometimes, these questions are difficult for the user to understand(they are non-english). So most of the time they want to copy paste the sentence into google translate. However, since this code is running in the command prompt,they have to select the text and using "right click --> copy" they can copy the text into google translate. Sometimes, by mistake the press "ctrl+c"(it is natural for everyone to use this combination for copying). Doing this will terminate the code, and they have to start over. I need to know I can prevent this from happening. In other words, if they press "ctrl+c" nothing happens and my software doesn't abort.
thanks
import signal
def SigIntHand(SIG, FRM):
print("Please Right click-copy. Ctrl-C does not work on the cmd prompt")
signal.signal(signal.SIGINT, SigIntHand)
or if you want it completely ignored:
import signal
signal.signal(signal.SIGINT, signal.SIG_IGN)
When you hit ctrl+c it sends SIGINT to the running process. You can catch it as described here.
You can find more about the different types of signals here.
If using X, the text is normally copied to the clipboard once it's selected. Just paste it using middle mouse button or Shift+insert.

Robustly killing Windows programs stuck reporting 'problems'

I am looking for a means to kill a Windows exe program that, when being tested from a python script, crashes and presents a dialog to the user; as this program is invoked many times, and may crash repeatedly, this is not suitable.
The problem dialog is the standard reporting of a Windows error:
"Foo.exe has encountered a problem and needs to close. We are sorry for the inconvenience"
and offers a Debug, Send Error Report, and Don't Send buttons.
I am able to kill other forms of dialog resulting from crashes (e.g. a Debug build's assert failure dialog is OK.)
I have tried taskkill.exe, pskill, and the terminate() function on the Popen object from the subprocess module that was used to invoke the .exe
Has anyone encountered this specific issue, and found a resolution?
I expect automating user input to select the window, and press the "Don't Send" button is one possible solution, but I would like something far simpler if possible
Wouldn't it be easier to disable the error reporting feature?
If you were to use CreateProcessEx or a WinAPI specific function, you might be able to call TerminateProcess or TerminateThread to forcibly end the process.

Categories

Resources