Redirecting console output to a Python string

Possible duplicate:
How can I write the output of stdout of a child process?

I run the cat program in bash from Python:

  import os os.system('cat foo.txt') 

How to get shell command output back in a Python script, something like:

  s = somefunction('cat foo.txt') 

?

UPD : Here is the associated stream.

+4
source share
1 answer

Use the subprocess module.

 from subprocess import Popen, PIPE (stdout, stderr) = Popen(["cat","foo.txt"], stdout=PIPE).communicate() print stdout 
+16
source

Source: https://habr.com/ru/post/1286218/


All Articles