2009-06-15 4 views
4

Можно создать дубликат:
How can I capture the stdout output of a child process?Перенаправление вывода консоли в строку Python

Я бегу программу cat -как в Баш из Python:

import os 

    os.system('cat foo.txt') 

Как я получить вывод команды оболочки обратно в сценарии Python, что-то вроде:

s = somefunction('cat foo.txt') 

?

UPD: Here - связанная нить.

+0

dupe ... http://stackoverflow.com/questions/923079/subprocess-and-stdout/923108 –

ответ

16

Используйте модуль subprocess.

from subprocess import Popen, PIPE 

(stdout, stderr) = Popen(["cat","foo.txt"], stdout=PIPE).communicate() 
print stdout 
Смежные вопросы