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

2012-04-17 Thread Edward d'Auvergne
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


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

2012-04-17 Thread Edward d'Auvergne
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  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 
> 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