Login | Register For Free | Help
Search for: (Advanced)

Mailing List Archive: Python: Python

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

 

 

Python python RSS feed   Index | Next | Previous | View Threaded


edward at nmr-relax

Apr 17, 2012, 7:21 AM

Post #1 of 6 (168 views)
Permalink
How to determine if IO redirection is occurring with the output from a Python program?

Hi,

I was wondering if anyone knows of how to detect when IO redirection
of any form is happening within a Python program? I would like to
emulate the behaviour of the GNU tools (for example the Unix commands
'ls' or 'grep') whereby ascii escape sequences are printed if the
output is solely to the terminal, and in all other cases (redirection
to file via '>', pipes via '|', or more complex redirections) the
ascii escape characters are suppressed. Any ideas would be
appreciated!

Cheers,

Edward


--
Edward d'Auvergne, PhD
Lead developer of the projects relax, minfx, and bmrblib
http://www.nmr-relax.com
http://gna.org/projects/minfx
http://gna.org/projects/bmrblib
--
http://mail.python.org/mailman/listinfo/python-list


pkugrinas at gmail

Apr 17, 2012, 7:29 AM

Post #2 of 6 (163 views)
Permalink
Re: How to determine if IO redirection is occurring with the output from a Python program? [In reply to]

Check os.isatty(fd). It will return True if fd is a terminal-like device.

On Tue, Apr 17, 2012 at 5:21 PM, Edward d'Auvergne <edward [at] nmr-relax>wrote:

> Hi,
>
> I was wondering if anyone knows of how to detect when IO redirection
> of any form is happening within a Python program? I would like to
> emulate the behaviour of the GNU tools (for example the Unix commands
> 'ls' or 'grep') whereby ascii escape sequences are printed if the
> output is solely to the terminal, and in all other cases (redirection
> to file via '>', pipes via '|', or more complex redirections) the
> ascii escape characters are suppressed. Any ideas would be
> appreciated!
>
> Cheers,
>
> Edward
>
>
> --
> Edward d'Auvergne, PhD
> Lead developer of the projects relax, minfx, and bmrblib
> http://www.nmr-relax.com
> http://gna.org/projects/minfx
> http://gna.org/projects/bmrblib
> --
> http://mail.python.org/mailman/listinfo/python-list
>


rosuav at gmail

Apr 17, 2012, 7:31 AM

Post #3 of 6 (164 views)
Permalink
Re: How to determine if IO redirection is occurring with the output from a Python program? [In reply to]

On Wed, Apr 18, 2012 at 12:21 AM, Edward d'Auvergne
<edward [at] nmr-relax> wrote:
> I was wondering if anyone knows of how to detect when IO redirection
> of any form is happening within a Python program?  I would like to
> emulate the behaviour of the GNU tools (for example the Unix commands
> 'ls' or 'grep') whereby ascii escape sequences are printed if the
> output is solely to the terminal, and in all other cases (redirection
> to file via '>', pipes via '|', or more complex redirections) the
> ascii escape characters are suppressed.  Any ideas would be
> appreciated!

What you want is the "is-a-TTY" query, which is available in Python as
a method on the file-like object:

import sys
if sys.stdout.isatty(): # True if console, False if redirected
# do your fancy escape character stuff

Tip: Like the GNU tools, make this only a default. For instance, both
ls and grep have an option --color=WHEN where WHEN is either "never",
"always", or "auto". If you choose auto (which you can make the
default), the tools then check if stdout is a TTY.

ChrisA
--
http://mail.python.org/mailman/listinfo/python-list


edward at nmr-relax

Apr 17, 2012, 7:59 AM

Post #4 of 6 (167 views)
Permalink
Re: How to determine if IO redirection is occurring with the output from a Python program? [In reply to]

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.

Cheers,

Edward



On 17 April 2012 16:29, pat <pkugrinas [at] gmail> wrote:
> Check os.isatty(fd). It will return True if fd is a terminal-like device.
>
> On Tue, Apr 17, 2012 at 5:21 PM, Edward d'Auvergne <edward [at] nmr-relax>
> wrote:
>>
>> Hi,
>>
>> I was wondering if anyone knows of how to detect when IO redirection
>> of any form is happening within a Python program?  I would like to
>> emulate the behaviour of the GNU tools (for example the Unix commands
>> 'ls' or 'grep') whereby ascii escape sequences are printed if the
>> output is solely to the terminal, and in all other cases (redirection
>> to file via '>', pipes via '|', or more complex redirections) the
>> ascii escape characters are suppressed.  Any ideas would be
>> appreciated!
>>
>> Cheers,
>>
>> Edward
>>
>>
>> --
>> Edward d'Auvergne, PhD
>> Lead developer of the projects relax, minfx, and bmrblib
>> http://www.nmr-relax.com
>> http://gna.org/projects/minfx
>> http://gna.org/projects/bmrblib
>> --
>> http://mail.python.org/mailman/listinfo/python-list
>
>

--
Edward d'Auvergne, PhD
Lead developer of the projects relax, minfx, and bmrblib
http://www.nmr-relax.com
http://gna.org/projects/minfx
http://gna.org/projects/bmrblib
--
http://mail.python.org/mailman/listinfo/python-list


rosuav at gmail

Apr 17, 2012, 5:52 PM

Post #5 of 6 (159 views)
Permalink
Re: How to determine if IO redirection is occurring with the output from a Python program? [In reply to]

On Wed, Apr 18, 2012 at 12:59 AM, Edward d'Auvergne
<edward [at] nmr-relax> 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
--
http://mail.python.org/mailman/listinfo/python-list


drsalists at gmail

Apr 18, 2012, 2:06 PM

Post #6 of 6 (157 views)
Permalink
Re: How to determine if IO redirection is occurring with the output from a Python program? [In reply to]

On Tue, Apr 17, 2012 at 5:52 PM, Chris Angelico <rosuav [at] gmail> wrote:

>
> 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.
>

Last I heard, you could set up ANSI escape sequences using a line in
config.sys. This was back in the MS-DOS/PC-DOS days with command.com.
I've not tried it on cmd.exe or powershell.

Python python RSS feed   Index | Next | Previous | View Threaded
 
 


Interested in having your list archived? Contact Gossamer Threads
 
  Web Applications & Managed Hosting Powered by Gossamer Threads Inc.