Next: Contextual menus on object directories only, Previous: Spawning external processes, Up: Python FAQ

##### 16.8.7.3 Redirecting the output of spawned processes

In general, it is possible to redirect the output of any Python script to any GPS window (either an already existing one, or creating one automatically), through the `"output"` attribute of XML configuration files.

However, there is a limitation in python that the output of processes spawned through os.exec() or os.spawn() is redirected to the standard output, and not to the usual python output that GPS has overriden.

There are two solutions for this:

- Execute the external process through a pipe

  The output of the pipe is then redirected to Python's output, as in:

                import os, sys
                def my_external():
                   f = os.popen ('ls')
                   console = GPS.Console ("ls")
                   for l in f.readlines():
                      console.write ('   ' + l)

  This solution allows you, at the same time, to modify the output, for instance to indent it as in the example above.

- Execute the process through GPS

  You can go through the process of defining an XML customization string for GPS, and execute your process this way, as in:

                GPS.parse_xml ("""
                   <action name="ls">
                     <external output="output of ls">ls</external>
                   </action>""")

                def my_external():
                   GPS.execute_action ("ls")

  This solution also allows you to send the output to a different window than the rest of your script. But you cannot filter or modify the output as in the first solution.