0

I tried this:

import os

if os.system('somecommand') == 'output from command':

    do_something()

But it didn't worked. How can I do it? What library should I use?

When I use solution with subprocess below I get this errorcode:

Traceback (most recent call last):
  File "", line 1, in 
  File "/usr/lib/python2.7/subprocess.py", line 537, in check_output
    process = Popen(stdout=PIPE, *popenargs, **kwargs)
  File "/usr/lib/python2.7/subprocess.py", line 679, in __init__
    errread, errwrite)
  File "/usr/lib/python2.7/subprocess.py", line 1259, in _execute_child
    raise child_exception
OSError: [Errno 2] No such file or directory
zuberuber
  • 3,851
  • 2
  • 16
  • 20

1 Answers1

3

The return value of os.system() is the exit code of the process, not whatever it outputs to stdin. You want to use the subprocess module instead:

import subprocess

if subprocess.check_output('somecommand') == 'output from command':
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343