Python Forum

Full Version: how to do comparison for the value returned by os.system
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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.
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
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.