I have a CLI script and want it to read data from a file. It should be able to read it in two ways :
cat data.txt | ./my_script.py
./my
There is no reliable way to detect if sys.stdin is connected to anything, nor is it appropriate do so (e.g., the user wants to paste the data in). Detect the presence of a filename as an argument, and use stdin if none is found.
sys.stdin