1

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

1
  • Use the subprocess module to execute shell commands from Python and capture the output. Commented Jun 20, 2022 at 14:13

1 Answer 1

2

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.