Reputation: 13
How to capture bash command output using python script.
For eg:
running below in linux :
[root@xxxxxx oracle]# echo sumit
sumit
[root@xxxxxx oracle]#
How can i re print only the above output using python script ? like running python test.py shoud give 'sumit' as output. i tried below:
test.py :
import sys
sys.stdout.flush()
out = sys.stdin.readline()
print(out)
Above prints only the input i type but not the already displayed output
Upvotes: 1
Views: 1052
Reputation: 154
With subprocess
, you can run commands and check their return code, stdout and stderr outputs. Would that help?
For example:
import subprocess as proc
byte_output = proc.check_output(["ls", "-1", "."])
str_output = str(byte_output, "utf-8")
print(str_output)
# prints my local folders dev\ngit
Upvotes: 2