How to get stdout into a string (Python) [duplicate]

别来无恙 提交于 2019-12-07 18:49:58

问题


I need to capture the stdout of a process I execute via subprocess into a string to then put it inside a TextCtrl of a wx application I'm creating. How do I do that?

EDIT: I'd also like to know how to determine when a process terminates


回答1:


From the subprocess documentation:

from subprocess import *
output = Popen(["mycmd", "myarg"], stdout=PIPE).communicate()[0]



回答2:


Take a look at the subprocess module.

http://docs.python.org/library/subprocess.html

It allows you to do a lot of the same input and output redirection that you can do in the shell.

If you're trying to redirect the stdout of the currently executing script, that's just a matter of getting a hold of the correct file handle. Off the top of my head, stdin is 0, stdout is 1, and stderr is 2, but double check. I could be wrong on that point.



来源:https://stackoverflow.com/questions/3472760/how-to-get-stdout-into-a-string-python

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!