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

mike needpassion at gmail.com
Thu Jun 14 21:03:35 CEST 2007


On Jun 14, 2:55 am, Michael Hoffman <cam.ac... at mh391.invalid> wrote:
> Gabriel Genellina wrote:
> > En Wed, 13 Jun 2007 21:47:16 -0300, mike <needpass... 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

Really helps. Thanks Michael




More information about the Python-list mailing list