grabbing return codes from os.system() call

Donn Cave donn at oz.net
Thu Mar 8 01:34:32 EST 2001


Quoth Damian Menscher <menscher+python at uiuc.edu>:
| I'm new to Python (as in, my experience is essentially the tutorial),
| but I've already come up with an interesting question:
|
| How do I get the return code from an os.system call?  I would have
| expected I could do something like
|
| ---returncode---
| #/bin/csh
| echo do stuff
| exit 3
|
| and then in my python program I could do
|
| print os.system('./returncode')
|
| But it prints out 768.  Not particularly useful, even after I recognize
| the trick of dividing by 256 (byte-swapping going on?  No, because a
| return code of 768 reports as 0).  Given that my real return codes
| will be (possibly large) integers, this limitation will likely cause
| some serious problems down the line.
|
| Even better would be a way of returning a string (the script I run
| can be something other than csh, but it has to be a separate script).
|
| Ideas?  I'm trying to avoid writing the string out to a file and then
| read the file back in to the python program....

Your return codes will not be larger than 255, that's all the bits
there are for this kind of thing (on UNIX, anyway.)  If you have a
bigger number, you will have to put it somewhere else.  So writing
it out to a file might not be such a bad idea.

	Donn Cave, donn at oz.net



More information about the Python-list mailing list