By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,953 Members | 1,562 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,953 IT Pros & Developers. It's quick & easy.

Catching external program exceptions

P: n/a
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
Share this Question
Share on Google+
1 Reply


P: n/a
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.