all Python 3fileinputfunction
fileinput.isstdin()
reading state
Returns True if the last line was read from standard input rather than from a file.
Syntax
fileinput.isstdin() -> bool
Standard input is used when the file list is empty or contains '-'; filename() then returns '<stdin>'. inplace=True does not apply to stdin — the output goes to the screen, not to a file — and isstdin() helps notice that. A second pass over stdin yields no lines: the stream is not rewound.
Examples
import fileinput, io, sys
open('a.txt', 'w', encoding='utf-8').write('a1\n')
sys.stdin = io.StringIO('typed\n')
with fileinput.input(['a.txt', '-'], encoding='utf-8') as f:
for line in f:
print(fileinput.filename(), fileinput.isstdin()) # → a.txt False / <stdin> True
See also
Open in the full glossarydocs.python.orgReport an inaccuracy