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

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

or

sys.stdout.isatty()