I have code like this os.system(mycommand). I want to get result of batch command as string.How can i do this?
1 Answer
Python's subprocess module has a built-in function to do this as of Python 2.7.
import subprocess
try:
mycommand = "ls"
result = subprocess.check_output(mycommand, shell=True)
print(result)
except subprocess.CalledProcessError as error:
# Handle the error
print("Error: Command exited with code {0}".format(error.returncode))