os.system() question
Ethan Furman
ethan at stoneleaf.us
Mon Oct 19 14:23:01 EDT 2009
Bryan Irvine 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
>
>>>>if (os.system('echosadf test')):
>
> ... print 'success'
> ... else:
> ... print 'failed'
> ...
> sh: echosadf: command not found
> success
>
If I had to guess (which I am, since I'm not looking at the docs), I
would say that os.system returns the exit code of whatever it ran, and
since the exit code on success is almost always 0, and since 0 means
False in Python, you get 'failed' the first time around; likewise, a
non-zero exit code usually indicates a problem of some sort, and
non-zero is True in Python, hence the 'success' the second time around.
I wonder how the docs phrase it?
~Ethan~
More information about the Python-list
mailing list