开发者

redirecting standard output to the calling process

Im开发者_StackOverflow中文版 no bash expert so bear with me

I have a python script thats starting other processes, which then emit log messages on stdout.

what bash command would I use to redirect stdout of those child processes back to the stdout of the parent process (the python script thats starting the processes)?

thank you in advance


If you just want to capture the output of the child processes in your python script, the best way to do it would be to use the subprocess module:

import subprocess

p = subprocess.Popen(['/bin/ls', '-l'], stdout=subprocess.PIPE)
(out, _) = p.communicate()
print "Output:", out


I'm assuming you're using os.system("command") to execute your scripts. This method only executes the command and gives you the program's return value and NOT it's stdout. What you need is the subprocess module. For example:

import subprocess

proc = subprocess.Popen(['command', 'option 1', 'option 2'], stdout=subprocess.PIPE)
output = proc.communicate()[0] #for stdout. Use proc.communicate[1] for stderr

On looking at your comment, your solution is simple:

subprocess.Popen('command option 1 option 2', shell=True)

Note: command needs to be passed as a string. A sequence doesn't work.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜