The easiest automatic decision between defined input file or stdin and output file or stdout I have come up with in Python is:
import sys
# If intab (input) is defined, read it; if not assume stdin
if intab:
f = open(intab, "r")
else:
f = sys.stdin
# If otab (output) is defined, read it; if not assume stdout
if otab:
fout = open(otab, "w")
else:
fout = sys.stdout
# Do whatever you want
for line in f:
print(line)
write.fout(line)
# And close files, just to be safe
if f is not sys.stdin:
f.close()
if fout is not sys.stdout:
fout.close()
There might be something easier but this is simple and obvious.