how to redirect output from subprocess to standard textctrl ?

hello,

Can anyone explain to me how I should redirect the output of a subprocess call to some kind of textctrl (either TextCtrl or STC ) /

I tried this
  stdOUT = my_TextCtrl
  return subprocess.call ( arguments,
                            cwd = cwd ,
                            stdout = stdOUT, stderr = stdOUT,
                            shell = True )

But I get the following error :

Traceback (most recent call last):
  File "D:\Data_Python_25\PyLab_Works\PyLab_Works_Overview.py", line 179, in B_Run_Click
    Run_Python_Wait ( [ script ], stdOUT = self.Log )
  File "../support\system_support.py", line 349, in Run_Python_Wait
    shell = True )
  File "P:\Python\lib\subprocess.py", line 444, in call
    return Popen(*popenargs, **kwargs).wait()
  File "P:\Python\lib\subprocess.py", line 587, in __init__
    errread, errwrite) = self._get_handles(stdin, stdout, stderr)
  File "P:\Python\lib\subprocess.py", line 715, in _get_handles
    c2pwrite = msvcrt.get_osfhandle(stdout.fileno())
AttributeError: 'TextCtrl' object has no attribute 'fileno'

So I guess I don't understand how Pipes work :frowning:

thanks,
Stef Mientki

Don’t try to write directly to a control - it almost certainly won’t work the way you want it to. If the subprocess call is going to finish quickly, do this:

c = subprocess.Popen(arguments,
cwd = cwd ,
stdout = subprocess.PIPE, stderr = subprocess.PIPE,

shell = True) # actually, this can usually be False

(stdout, stderr) = c.communicate()

then collect the string data from stdout/stderr, then call my_TextCtrl.AppendText()

If it’s not going to finish quickly, start reading here:

http://wiki.wxpython.org/LongRunningTasks

This probably needs to be updated and/or expanded. To summarize, however, you would need to post custom events (to the main window, or just the textctrl itself) from a separate thread which calls subprocess.Popen, catches the output, and embeds it in the events.

···

On Wed, Oct 8, 2008 at 4:51 PM, Stef Mientki s.mientki@ru.nl wrote:

hello,

Can anyone explain to me how I should redirect the output of a subprocess call to some kind of textctrl (either TextCtrl or STC ) /

I tried this

stdOUT = my_TextCtrl

return subprocess.call ( arguments,

                       cwd   = cwd ,

                       stdout = stdOUT, stderr = stdOUT,

                       shell = True )

But I get the following error :
. . .

So I guess I don’t understand how Pipes work :frowning:

Hi Stef,

Can anyone explain to me how I should redirect the output of a subprocess
call to some kind of textctrl (either TextCtrl or STC ) /

I tried this
stdOUT = my_TextCtrl
return subprocess.call ( arguments,
                          cwd = cwd ,
                          stdout = stdOUT, stderr = stdOUT,
                          shell = True )

You can only redirect to file-like objects, whether that's an actual
file, a StringIO object, or something else. If I recall correctly
(and that mightn't be too well at the moment) you need an object that
supports at least a .write(message) and .flush() method. I think
examples with the logging module show how to redirect stdout/stderr to
a logging object ... you might get some insight out of those. If you
want to write directly to a text control, I suppose you could subclass
the text control and add .write and .flush methods in order to make it
compatible. I haven't done such a thing but I'm sure someone wiser
and more experienced on this list can give you better details.

···

thanks guys,
that was indeed the trick.

cheers,
Stef

Nathaniel Echols wrote:

···

On Wed, Oct 8, 2008 at 4:51 PM, Stef Mientki <s.mientki@ru.nl > <mailto:s.mientki@ru.nl>> wrote:

    hello,

    Can anyone explain to me how I should redirect the output of a
    subprocess call to some kind of textctrl (either TextCtrl or STC ) /

    I tried this
     stdOUT = my_TextCtrl
     return subprocess.call ( arguments,
                              cwd = cwd ,
                              stdout = stdOUT, stderr = stdOUT,
                              shell = True )

    But I get the following error :
    . . .
    So I guess I don't understand how Pipes work :frowning:

Don't try to write directly to a control - it almost certainly won't work the way you want it to. If the subprocess call is going to finish quickly, do this:

c = subprocess.Popen(arguments,
                          cwd = cwd ,
                          stdout = subprocess.PIPE, stderr = subprocess.PIPE,
                          shell = True) # actually, this can usually be False
(stdout, stderr) = c.communicate()

then collect the string data from stdout/stderr, then call my_TextCtrl.AppendText()

If it's not going to finish quickly, start reading here:

LongRunningTasks - wxPyWiki

This probably needs to be updated and/or expanded. To summarize, however, you would need to post custom events (to the main window, or just the textctrl itself) from a separate thread which calls subprocess.Popen, catches the output, and embeds it in the events.
------------------------------------------------------------------------

_______________________________________________
wxpython-users mailing list
wxpython-users@lists.wxwidgets.org
http://lists.wxwidgets.org/mailman/listinfo/wxpython-users

c = subprocess.Popen(arguments,
                          cwd = cwd ,
                          stdout = subprocess.PIPE, stderr = subprocess.PIPE,
                          shell = True) # actually, this can usually be False

"# actually, this can usually be False",
I don't think so,
if I make it False,
for every test I launch a command window popups,
which I need to close manually,
and sometimes I launch a few hundred scripts :wink:

cheers,
Stef

You can only redirect to file-like objects, whether that’s an actual

file, a StringIO object, or something else. If I recall correctly

(and that mightn’t be too well at the moment) you need an object that

supports at least a .write(message) and .flush() method. I think

examples with the logging module show how to redirect stdout/stderr to

a logging object … you might get some insight out of those. If you

want to write directly to a text control, I suppose you could subclass

the text control and add .write and .flush methods in order to make it

compatible. I haven’t done such a thing but I’m sure someone wiser

and more experienced on this list can give you better details.

I’ve tried this before - in theory, you are correct about adding write and flush methods, but in practice, it’s a mess. The GUI basically locks up until the subprocess is complete. If it’s instantaneous this may not be a problem, otherwise you’ll need to start a separate thread and run subprocess.Popen and post events from there.