How to find out if a script works on tty?

I would like my script to act differently in an interactive shell session and when launched with a redirected stdout (for example, when it was passed to another command).

How to find out which of these two happens in a Python script?

An example of this behavior in an existing program: grep --color = auto highlights matches when launched in the interactive shell, but not when transferred to something else.

+48
python shell
May 13, '09 at 15:24
source share
1 answer
import os, sys os.isatty(sys.stdout.fileno()) 

or

 sys.stdout.isatty() 
+57
May 13, '09 at 15:24
source share



All Articles