Use the os.isatty()
function on the filedescriptor of the stdout
stream or any other file you need to test:
>>> import sys, os
>>> os.isatty(sys.stdout.fileno())
True
For open files (like the sys.stdout
stream) the .fileno()
method returns the necessary descriptor.