exception KeyboardInterrupt and os.system command

Diez B. Roggisch deets at nospam.web.de
Mon Nov 28 00:18:17 CET 2005

darren kirby wrote:
> Hello all.
> I have a python script here which is just a wrapper for 2 or more system 
> commands. I would estimate the program spends at least 95.5% of 'real' time 
> running the system commands.
> I want to trap the [crtl-c] key combo and exit (somewhat) gracefully if the 
> user decides to abort the program. I am using os.system for the system call, 
> and I have wrapped the entire main loop in a try: except KeyboardInterrupt 
> statement to try to attain these ends.
> As it is though, if the program is currently in the system command, only that 
> system command is terminated, and the next loop of my program starts.
> Is there a way to make this work? ie: terminate the entire script? Will popen 
> do this? I don't really want to use popen because all I need is for the 
> system command to run, and check the exit status. Also, popen will pooch the 
> output of the system commands (which I want to be printed to the console) 
> because the system commands (faad, mpg123, and oggenc) have buffered output 
> which won't properly be displayed if I simply print each line of the file 
> object returned by popen. 

 From "man system":

   The system() function hands the argument string to the command inter-
      preter sh(1).  The calling process waits for the shell to finish 
      ing the command, ignoring SIGINT and SIGQUIT, and blocking SIGCHLD.

So - I guess its popen2 or nothing :)



More information about the Python-list mailing list