471,306 Members | 851 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

Catching external program exceptions

I need to catch exceptions thrown by programs started by the os.system function,
as indicated by a non-zero return code (e.g. the mount utility). For example,
if I get the following results in a bash shell:

$mount test
mount: can't find /home/marty/test in /etc/fstab or /etc/mtab

then I want to catch the same exception from the corresponding os.system() call,
i.e. "os.system('mount test')", but it doesn't work as expected:

>>import os, sys
try: os.system('mount test')
.... except: print 'error'
....
mount: can't find /home/marty/test in /etc/fstab or /etc/mtab
256
>>>
I get the same results with popon, popen2, popen3, etc. Apparently these also
work only when the program does not generate an exception. Is there any way to
catch the return code. or if not, a workaround?
Jan 1 '08 #1
1 2312
En Tue, 01 Jan 2008 20:57:44 -0200, Marty <ma*****@earthlink.netescribi�:
I need to catch exceptions thrown by programs started by the os.system
function,
as indicated by a non-zero return code (e.g. the mount utility). For
Notice that these are two different things. You can wait until the
external program ends, and get its return code; and you can sometimes read
a message from its stderr. But none of these things by itself throws an
exception in Python.
example,
if I get the following results in a bash shell:

$mount test
mount: can't find /home/marty/test in /etc/fstab or /etc/mtab

then I want to catch the same exception from the corresponding
os.system() call,
i.e. "os.system('mount test')", but it doesn't work as expected:
Perhaps it doesn't work as *you* expected, but it does work as specified.
From the os.system description at
http://docs.python.org/lib/os-process.html
"On Unix, the return value is the exit status of the process encoded in
the format specified for wait(). [...] [That return value] is
system-dependent."
Later on the same page, describing the wait function, says that the return
value is "a 16-bit number, whose low byte is the signal number that killed
the process, and whose high byte is the exit status (if the signal number
is zero)"
So os.system does NOT raise an exception when the executed program ends
with a non-zero exit code.
I get the same results with popon, popen2, popen3, etc. Apparently
these also
work only when the program does not generate an exception.
An external program cannot generate an exception inside the Python
program. Only Python itself can do that.
Is there any way to
catch the return code. or if not, a workaround?
From the description above, you could do some math to obtain the exit code
looking at the os.system return value.
But perhaps it's easier to use subprocess.check_call, see "Convenience
functions" at http://docs.python.org/lib/module-subprocess.html

--
Gabriel Genellina

Jan 2 '08 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

4 posts views Thread by Laxmikant Rashinkar | last post: by
3 posts views Thread by Manish Soni | last post: by
7 posts views Thread by cmay | last post: by
5 posts views Thread by Simon Tamman | last post: by
3 posts views Thread by john | last post: by
reply views Thread by rosydwin | 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.