Python Forum
how to do comparison for the value returned by os.system - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: how to do comparison for the value returned by os.system (/thread-6209.html)



how to do comparison for the value returned by os.system - jash - Nov-10-2017

In my code, I’m trying to compare the output returned by the os.system(’/opt/abc/cmd/abc-target’)

Here’s my code:

import os
s= os.system(’/opt/abc/cmd/abc-target’)
if (s == ‘abc’ ):
print (‘great you found the target system’)

The above code doesn’t throw any error but doesn’t execute the if statement. I want the comparison to match and print success.
Please help me understand what I'm doing wrong.


RE: how to do comparison for the value returned by os.system - heiner55 - Nov-11-2017

This is how os.system() work.
You get only a number back (0==success (else error)).
See https://docs.python.org/3/library/os.html#os.system


RE: how to do comparison for the value returned by os.system - snippsat - Nov-11-2017

Avoid os.system() by all means(security reasons),and use subprocess instead:
Quote:os.system() and os.popen*() have been deprecated since Python 2.6 in favor of the subprocess module.