Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to recognize whether a script is running on a tty?

Tags:

python

shell

I would like my script to act differently in an interactive shell session and when running with redirected stdout (for example when piped to some other command).

How do I recognize which of these two happen in a Python script?

Example of such behavior in existing program: grep --color=auto highlights matches when running in interactive shell, but doesn't when piped to something else.

like image 817
Paweł Hajdan Avatar asked May 13 '09 15:05

Paweł Hajdan


1 Answers

import os, sys os.isatty(sys.stdout.fileno()) 

or

sys.stdout.isatty() 
like image 73
jdizzle Avatar answered Oct 06 '22 00:10

jdizzle