How to compare os.system or subprocess output

python

#1

<PLEASE USE THE FOLLOWING TEMPLATE TO HELP YOU CREATE A GREAT POST!>

This is not specific to the exercise. I’m working on the project and I’m stuck when running the python script.

In my code, I’m trying to extract the output but I’m not able to do comparison with the string.

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.


#2

Reading the documentation for the function you’re using will explain why you don’t get the printed result back:
https://docs.python.org/3/library/os.html#os.system

As for what you’re trying to do, it’s a simple thing to google for:
https://www.google.com/search?q=python+subprocess+capture+stdout


#3

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

also, I tred

import subprocess
Platform = subprocess.check_output([’/opt/abc/cmd/abc-target’])
if Platform == ‘abc’:
print 'great you found abc system’
elif Platform == ‘def’:
print 'great you found def system’
else:
print ‘No system found’

I know it returns reponse 0 but in my case there are 2 options returned by the
s= os.system(’/opt/abc/cmd/abc-target’) depending upon the system.
If text matched return by the text I need to execute the code.


#4

I don’t know what you mean to say.


#5

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.