0

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 1

4

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))
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.