##### 16.8.7.5 Redirecting the output to specific windows

By default, the output of all python commands is displayed in the Python console. However, you might want in some cases to create other windows in GPS for this output. This can be done in one of two ways:

- Define a new action

  If the whole output of your script should be redirected to the same window, or if the script will only be used interactively through a menu or a key binding, the easiest way is to create a new XML action, and redirect the output, as in

                <?xml version="1.0" ?>
                <root>
                  <action name="redirect output" output="New Window">
                     <shell lang="python">print "a"</shell>
                  </action>
                </root>

  All the various shell commands in your action can be output in a different window, and this also applies for the output of external commands.

- Explicit redirection

  If, however, you want to control in your script where the output should be sent, for instance if you can't know that statically when you write your commands, you can use the following code:

                sys.stdin = sys.stdout = GPS.Console ("New window")
                print "foo"
                print (sys.stdin.read ())
                sys.stdin = sys.stdout = GPS.Console ("Python")

  The first line redirect all input and output to a new window, which is created if it doesn't exist yet. Note however that the output of stderr is not redirected, and you need to explicitely do it for `sys.stderr`.

  The last line restore the default Python console. You must do this at the end of your script, or all scripts will continue to use the new consoles.

  You can alternatively create separate objects for the output, and use them in turn:

                my_out  = GPS.Console ("New Window")
                my_out2 = GPS.Console ("New Window2")

                sys.stdout=my_out
                print "a"
                sys.stdout=my_out2
                print "b"
                sys.stdout=GPS.Console ("Python")

  The parameter to the constructor `GPS.Console` indicates whether any output sent to that console should be saved by GPS, and reused for the `%1`, `%2`, ... parameters if the command is executed in a GPS action. That should generally be 1, except for stderr where it should be 0.