I am trying to check the usage of check_output using he below script and running into compilation error,where am I going wrong?
import os
import subprocess
from subprocess import check_output
#result = subprocess.check_output(['your_program.exe', 'arg1', 'arg2'])
SCRIPT_ROOT=subprocess.check_output(["pwd","shell=True"])
print SCRIPT_ROOT
def main ():
pass
if __name__ == '__main__':
main()
Traceback (most recent call last):
File "test.py", line 3, in <module>
from subprocess import check_output
ImportError: cannot import name check_output
check_outputhas been introduced in Python 2.7. If you’re using an earlier version of python, it’s just not there.Alternative is to use
Popen.Proof of this can be found here.
Demo of the substitute.
Output
The only real difference is that
Popenwon’t throw an exception when command returns a non-zero code.