On Mon, Oct 19, 2009 at 2:14 PM, Bryan  Irvine <sparcta...@gmail.com> wrote:
> I'm a python n00b and so pardon me in advance if this is really stupid
> question.
>
> I have my suspicions but why does the following not work the way I'm
> anticipating it will?
>
> (python 2.4.4)
>
>>>> import os
>>>> if (os.system('echo test')):
> ...    print 'success'
> ... else:
> ...    print 'failed'
> ...
> test
> failed

A program returns 0 if it executed successfully, but python interprets
0 to mean false.

>>>> if (os.system('echosadf test')):
> ...    print 'success'
> ... else:n
> ...    print 'failed'
> ...
> sh: echosadf: command not found
> success

In this case, your program fails so it returns a non-zero exit code
(32512) which python interprets to mean true. Just switch the test to
if(not os.system(command)) and it will work fine. But you should
probably use the newer subprocess module instead.
http://docs.python.org/library/os.html#os.system :
"""
The subprocess  module provides more powerful facilities for spawning
new processes and retrieving their results; using that module is
preferable to using this function. Use the subprocess  module. Check
especially the Replacing Older Functions with the subprocess Module
section.
"""


>>>>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to