is input from a pipe?

Liste des GroupesRevenir à cl python 
Sujet : is input from a pipe?
De : kammamuri (at) *nospam* libero.it (km)
Groupes : comp.lang.python
Date : 15. Jan 2025, 19:31:34
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <vm8uu5$3269p$1@dont-email.me>
User-Agent : Pan/0.160 (Toresk; )
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))

Date Sujet#  Auteur
15 Jan 25 o is input from a pipe?1km

Haut de la page

Les messages affichés proviennent d'usenet.

NewsPortal