is input from a pipe?
km
kammamuri at libero.it
Wed Jan 15 13:31:34 EST 2025
Not a question, but a quick note about a problem that sometimes pops up in
forums, that is how to detect on Linux if standard input (or any I/O
stream) is via pipe. My suggestion is to check if the stream is a FIFO, if
True it is a pipe, otherwise not a pipe
The solution that sometimes is proposed, that is
if not sys.stdin.isatty()
simply checks if the input is not from a terminal, but it may be from a
file, not only from a pipe.
import os
import sys
import stat
def check_if_stream_is_pipe(ifile):
return stat.S_ISFIFO(os.fstat(ifile.fileno()).st_mode)
print(check_if_stream_is_pipe(sys.stdin))
More information about the Python-list
mailing list