En Tue, 01 Jan 2008 20:57:44 -0200, Marty <[EMAIL PROTECTED]> escribi�:
> 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 -- http://mail.python.org/mailman/listinfo/python-list