471,353 Members | 1,681 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,353 software developers and data experts.

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

Hi Guys,

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.

Here is the code:
===================================
#!/usr/bin/python
import os
import sys
import time

try:
fh = open("tt.py")
time.sleep(10)
#os.system("wrong_command_test")
except IOError:
print 'failed to open.'
sys.exit(0)
except:
print 'Some exceptions occurred.'
else:
print 'well',

print 'Done'

===================================
when the tt.py doesn't exist, the script printed:
failed to open.
when the tt.py exists, the script printed:
well done
when I press Ctrl-C while the program is sleeping, the script printed:
Some exceptions occurred.
Done

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
Any opinions would be appreciated.

Mike

Jun 14 '07 #1
3 5855
En Wed, 13 Jun 2007 21:47:16 -0300, mike <ne*********@gmail.comescribió:
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.
You can extract the exit status from what os.system returns (see the
details on the docs for os.system); in particular, usually "command not
found" is error 127
This is a list of more-or-less standard exit codes:
http://www.faqs.org/docs/abs/HTML/exitcodes.html

--
Gabriel Genellina

Jun 14 '07 #2
Gabriel Genellina wrote:
En Wed, 13 Jun 2007 21:47:16 -0300, mike <ne*********@gmail.comescribió:
>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
Jun 14 '07 #3
On Jun 14, 2:55 am, Michael Hoffman <cam.ac...@mh391.invalidwrote:
Gabriel Genellina wrote:
En Wed, 13 Jun 2007 21:47:16 -0300, mike <needpass...@gmail.comescribió:
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

Jun 14 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

21 posts views Thread by dkcpub | last post: by
24 posts views Thread by mag31 | last post: by
16 posts views Thread by Einar Høst | last post: by
5 posts views Thread by TerryWilson | last post: by
8 posts views Thread by SStory | last post: by
1 post views Thread by Anonieko | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.