How to determine if IO redirection is occurring with the output from a Python program?

Chris Angelico rosuav at gmail.com
Tue Apr 17 20:52:20 EDT 2012


On Wed, Apr 18, 2012 at 12:59 AM, Edward d'Auvergne
<edward at nmr-relax.com> wrote:
> Hi,
>
> Thanks for the incredibly quick responses!  The os.isatty() call (and
> sys.std*.isatty() calls as suggested by Chris Angelico at
> http://mail.python.org/pipermail/python-list/2012-April/1291048.html)
> work perfectly for this job!  I might have to do some testing later on
> Windows though to see what happens when the ansi escape characters are
> not supported.

isatty() is supported on Windows (the underlying C API is different,
but the beauty of a high-level language is that you no longer need to
care), but the standard Windows console doesn't support ANSI
sequences. I think there is a way to enable them, but I don't recall
it off hand. However, if you can pipe your output through a socket
connection, a MUD client can be your console. There are plenty around;
RosMud [1] is a quite light-weight one that I wrote some years ago,
and use constantly. Going for a MUD connection may feel a little
weird, but it's pretty easy, and it gives you instant cross-platform
networking capabilities.

ChrisA
[1] http://www.kepl.com.au/esstu/rosmud.html



More information about the Python-list mailing list