4

Possible Duplicate:
How can I capture the stdout output of a child process?

I'm running a cat-like program in bash from Python:

   import os

   os.system('cat foo.txt')

How do I get the output of the shell command back in the Python script, something like:

   s = somefunction('cat foo.txt')

?

UPD: Here is a related thread.

1

1 Answer 1

16

Use the subprocess module.

from subprocess import Popen, PIPE

(stdout, stderr) = Popen(["cat","foo.txt"], stdout=PIPE).communicate()
print stdout
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.