How can I capture all exceptions especially when os.system() fail? Thanks

Michael Hoffman cam.ac.uk at mh391.invalid
Thu Jun 14 06:55:15 EDT 2007


Gabriel Genellina wrote:
> En Wed, 13 Jun 2007 21:47:16 -0300, mike <needpassion at gmail.com> escribió:
> 
>> Following piece of code can capture IOError when the file doesn't
>> exist, also, other unknown exceptions can be captured when I press
>> Ctrl-C while the program is sleeping(time.sleep). Now the question is:
>> when I run the non-exist command, the exception cannot be captured.
> 
>> So far so good, then I changed the code to run a non-exist command
>> "wrong_command_test"(commented the open and sleep lines), then the
>> script printed:
>> sh: wrong_command_test: command not found
>> well Done
> 
> That's because it is not an exception, it is an error message coming 
> from your shell, not from Python.

Of course if you use subprocess.check_call() instead of os.system(), it 
will become an exception (CalledProcessError).
-- 
Michael Hoffman



More information about the Python-list mailing list