Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Need help with os.system in linux

2 views
Skip to first unread message

akshay bhat

unread,
Jan 16, 2009, 7:05:13 PM1/16/09
to
Hello
i am calling a program using os.system in python on Linux.
However in i found that program being executed and soon returned 256.
but when i ran it using terminal i got proper results.
Now in case of windows, python waits till the process is finished,
Can you please tell me how to implement this in linux?

excord80

unread,
Jan 16, 2009, 8:32:49 PM1/16/09
to

Maybe it runs faster in GNU/Linux? :)

Either way, you probably want to post some minimal example code if you
would like more help on this.

Thomas Bellman

unread,
Jan 17, 2009, 2:19:45 AM1/17/09
to
akshay bhat <aksha...@gmail.com> writes:

> i am calling a program using os.system in python on Linux.
> However in i found that program being executed and soon returned 256.
> but when i ran it using terminal i got proper results.

Under Linux (but unfortunately not generally under Unix), you can
interpret return code from os.system() using os.WIFSIGNALED() or
os.WIFEXITED(), and os.WEXITSTATUS() or os.WTERMSIG(). Something
like this:

status = os.system(commandline)
if os.WIFSIGNALED(status):
print "Command was killed by signal", os.WTERMSIG(status)
else:
print "Command exited with return status", os.WEXITSTATUS(status)

(os.WIFSIGNALED() and os.WIFEXITED() are each others inverses,
for this use case. In the above code, I could have used 'not
os.WIFEXITED(status)' instead of 'os.WIFSIGNALED(status)' without
changing its meaning.)

If you do that for your program, you will see that the program
you call are exiting with return status 1 (that's what a return
value of 256 from os.system() happens to mean), i.e it is calling
exit() with 1 as argument.

*Why* the program you are calling does that, is impossible to
tell from the information you have given us. You haven't even
told us what command line you are passing to os.system(), so if
you need more help, you must tell us more.


May I also suggest that you use the subprocess module instead of
os.system(). The subprocess module is a newer and better interface
for running external commands. For example, you can avoid having
to deal with quoting shell metacharacters, and interpreting the
return values are easier.


--
Thomas Bellman, Lysator Computer Club, Linköping University, Sweden
"Life IS pain, highness. Anyone who tells ! bellman @ lysator.liu.se
differently is selling something." ! Make Love -- Nicht Wahr!

0 new messages