You want to look at fileinput
Suppose I have this file:
$ cat bobsDetail.txt
File 'Bob's Detail'
I can write a simple loop that will either 1) process stdin or 2) open the file name and process the contents:
#!/usr/bin/python
import fileinput
for line in fileinput.input():
print line
Now make that executable (Unix):
$ chmod +x fi.py
Then you can process that any way it is presented:
$ ./fi.py bobsDetail.txt
File 'Bob's Detail'
Or,
$ cat bobsDetail.txt | ./fi.py
File 'Bob's Detail'
Then you can identify stdin vs file:
for line in fileinput.input():
if fileinput.isfirstline():
if fileinput.isstdin():
print 'stdin'
else:
print fileinput.filename()
print line
From a file
$ ./fi.py bobsDetail.txt
bobsDetail.txt
File 'Bob's Detail'
From stdin:
$ cat bobsDetail.txt | ./fi.py
stdin
File 'Bob's Detail'