Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #197191

is input from a pipe?

From km <kammamuri@libero.it>
Newsgroups comp.lang.python
Subject is input from a pipe?
Date 2025-01-15 18:31 +0000
Organization A noiseless patient Spider
Message-ID <vm8uu5$3269p$1@dont-email.me> (permalink)

Show all headers | View raw


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))

Back to comp.lang.python | Previous | Next | Find similar


Thread

is input from a pipe? km <kammamuri@libero.it> - 2025-01-15 18:31 +0000

csiph-web