• is input from a pipe?

    From km@21:1/5 to All on Wed Jan 15 18:31:34 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))

    --- SoupGate-Win32 v1.05
    * Origin: fsxNet Usenet Gateway (21:1/5)