How can I get output from the command line to state in a script?

I tried this:

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

But that did not work. How can I do it? Which library should I use?

When I use the solution with the subprocess below, I get this error code:

 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 
0
source share
1 answer

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

 import subprocess if subprocess.check_output('somecommand') == 'output from command': 
+3
source

Source: https://habr.com/ru/post/955559/


All Articles