Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

python

stdout

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

like image 562
Gabriele Cirulli Avatar asked Aug 16 '26 06:08

Gabriele Cirulli


2 Answers

From the subprocess documentation:

from subprocess import *
output = Popen(["mycmd", "myarg"], stdout=PIPE).communicate()[0]
like image 76
Blair Conrad Avatar answered Aug 18 '26 20:08

Blair Conrad


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.

like image 40
haydenmuhl Avatar answered Aug 18 '26 21:08

haydenmuhl



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!