What's the best way to tell if a Python program has anything to read from stdin?
Question: I want a program to do one thing if executed like this:

    
    
    cat something | my_program.py
    

and do another thing if run like this

    
    
    my_program.py
    

But if I read from stdin, then it will wait for user input, so I want to see
if there is anything to read before trying to read from stdin.


Answer: If you want to detect if someone is piping data into your program, or running
it interactively you can use isatty to see if stdin is a terminal:

    
    
    $ python -c 'import sys; print sys.stdin.isatty()'
    True
    $ echo | python -c 'import sys; print sys.stdin.isatty()'
    False
    

