How to find a command line program is piped from another program or that program was running directly with arguments? For example, wc command can be run as both ways.

# Create a file /tmp/hello.txt echo "Hello World" > /tmp/hello.txt # wc can be used as both ways, cat /tmp/hello.txt | wc # or wc /tmp/hello.txt

In Python, it is very easy to identify how a command is run

import sys if sys . stdin . isatty (): filename = sys . argv [ 1 ] # do something with file, open the file and process it else : # use for `line in sys.stdin` to read line by line data = sys . stdin . read () # do something with data