How do I check if stdin has some data?
Asked Answered
P

7

89

In Python, how do you check if sys.stdin has data or not?

I found that os.isatty(0) can not only check if stdin is connected to a TTY device, but also if there is data available.

But if someone uses code such as

sys.stdin = cStringIO.StringIO("ddd")

and after that uses os.isatty(0), it still returns True. What do I need to do to check if stdin has data?

Prescriptible answered 21/9, 2010 at 17:34 Comment(7)
what exactly you want to achieve ?Authentic
That's happening because os.isatty(0) checks if the file associated to the file descriptor (fd) 0 is a TTY. When you change the sys.stdin variable, you're not changing the file associated with fd0. fd0 is still pointing to the original stdin (which is a TTY in your case).Psychokinesis
Check this answer. Does it pertain to your question?Danforth
@Cristian Ciupitu,you are right,i use sys.stdin=cStringIO.String("ddd") to redirect the stdin,what i want is how to check sys.stdin is have data,if i use sys.stdin.read() directly it will block the below thing and wait always if no input givenPrescriptible
I fail to see how reading from cStringIO.StringIO("ddd") could block; it always has data available, except when EOF is reached of course.Psychokinesis
#3763381Burberry
It would be interesting to know how you found that os.isatty(0) can not only check if stdin is connected to a TTY device, but also if there is data available.Aileenailene
S
96

On Unix systems you can do the following:

import sys
import select

if select.select([sys.stdin, ], [], [], 0.0)[0]:
    print("Have data!")
else:
    print("No data")

On Windows the select module may only be used with sockets though so you'd need to use an alternative mechanism.

Social answered 21/9, 2010 at 18:23 Comment(9)
i'm sorry i have to remove the accept,because i use sys.stdin=cStringIO.StringIO("ddd") redirect the stdin to a mock file,but it didn't have fileno method,it i use you code it will raise if not select.select([sys.stdin],[],[],0.0)[0]: TypeError: argument must be an int, or have a fileno() method.Prescriptible
@mlzboy: only "real" files, opened by the operating system have a file descriptor number (btw on Unix sockets or pipes are files too). select which calls the operating system's select function can work only with those files. StringIO is a virtual file known only to the Python interpreter therefore it doesn't have a file descriptor and you can't use select on it.Psychokinesis
@Cristian Ciupitu thank you i got it,finally i use pipe replace StringIO,here is the code,may be help others r="\n".join(dict["list_result"].split("\n")[:i]) fdr,fdw=os.pipe() os.write(fdw,r) os.close(fdw) f=os.fdopen(fdr) sys.stdin=fPrescriptible
@Prescriptible pipes have a limited buffer size. Writing to a pipe with nothing reading from it is dangerous because if you try to write more than the buffer size it will block (deadlock).Pickford
This method failed for me when used in a script that was executed by cron. sys.stdin.isatty() however did work for me within cron.Buchanan
Does not work in Jenkins, returns <_io.TextIOWrapper name='<stdin>' mode='r' encoding='UTF-8'> and not []Phasis
This is dangerous. If the script is run with /dev/null as stdin, select.select will immediately return. This is the case when running the script via cron or other tools. The easiest way to replicate this behavior is to add < /dev/null on the command line.Subzero
This method tests only whether sys.stdin has data that are immediately available. And I don't think that is strictly guaranteed even when it is connected to a file (e.g. what if it is connected to a file on an NFS and the underlying network connection is spotty?). select is meant for multiplexing, i.e. choosing whatever is ready to read/write from multiple sources/destinations.Reena
This solution works. I had a requirement that the script calls input("prompt>") at some point. But if the user has already entered the text and pressed enter, input will consume the data, but will not add a newline. and the next print will be on the same line as the prompt>. With the select.select, I can print a newline from the code.Willi
M
99

I've been using

if not sys.stdin.isatty()

Here's an example:

import sys

def main():
    if not sys.stdin.isatty():
        print "not sys.stdin.isatty"
    else:
        print "is  sys.stdin.isatty"

Running

$ echo "asdf" | stdin.py
not sys.stdin.isatty

sys.stdin.isatty() returns false if stdin is not connected to an interactive input device (e.g. a tty).

isatty(...)
    isatty() -> true or false. True if the file is connected to a tty device.
Marj answered 18/7, 2013 at 23:39 Comment(7)
Thanks! This is so much easier than using select.Suave
Thanks. That solved my: echo 'maybe pipe' | stdin.py +maybe_args on linux. Does melancholynaturegirl's solution work on windows?Metacarpal
so it seems... C:\Users\naturegirl\Desktop>C:\Python27\python.exe isatty.py is sys.stdin.isatty C:\Users\naturegirl\Desktop> C:\Users\naturegirl\Desktop>echo "foo" | C:\Python27\python.exe isatty.py not sys.stdin.isattyMarj
This is not a good idea. What if stdin is being piped in from a file? isatty() is telling you if stdin is coming in directly from a terminal, not if there is more data to read from stdinLeet
Since grep is mentioned in another post, I'll mention that less uses isatty. I came here looking to do something like less: read input from a file if a file path is given as an input argument, but read input from stdin otherwise. An alternative is to use - as an alias for stdin and just accept the file argument.Piscatelli
I think this answers whether the input comes from a "human typing on a terminal who will then press Ctrl+D to send the input" or some "data stream" (stdin from pipe, file, etc.), whereas the other post with select answers whether the "data stream" has any more data in it.Oversell
Exactly what i want. Either pipe in some data or do default stuff...Exuberant
S
96

On Unix systems you can do the following:

import sys
import select

if select.select([sys.stdin, ], [], [], 0.0)[0]:
    print("Have data!")
else:
    print("No data")

On Windows the select module may only be used with sockets though so you'd need to use an alternative mechanism.

Social answered 21/9, 2010 at 18:23 Comment(9)
i'm sorry i have to remove the accept,because i use sys.stdin=cStringIO.StringIO("ddd") redirect the stdin to a mock file,but it didn't have fileno method,it i use you code it will raise if not select.select([sys.stdin],[],[],0.0)[0]: TypeError: argument must be an int, or have a fileno() method.Prescriptible
@mlzboy: only "real" files, opened by the operating system have a file descriptor number (btw on Unix sockets or pipes are files too). select which calls the operating system's select function can work only with those files. StringIO is a virtual file known only to the Python interpreter therefore it doesn't have a file descriptor and you can't use select on it.Psychokinesis
@Cristian Ciupitu thank you i got it,finally i use pipe replace StringIO,here is the code,may be help others r="\n".join(dict["list_result"].split("\n")[:i]) fdr,fdw=os.pipe() os.write(fdw,r) os.close(fdw) f=os.fdopen(fdr) sys.stdin=fPrescriptible
@Prescriptible pipes have a limited buffer size. Writing to a pipe with nothing reading from it is dangerous because if you try to write more than the buffer size it will block (deadlock).Pickford
This method failed for me when used in a script that was executed by cron. sys.stdin.isatty() however did work for me within cron.Buchanan
Does not work in Jenkins, returns <_io.TextIOWrapper name='<stdin>' mode='r' encoding='UTF-8'> and not []Phasis
This is dangerous. If the script is run with /dev/null as stdin, select.select will immediately return. This is the case when running the script via cron or other tools. The easiest way to replicate this behavior is to add < /dev/null on the command line.Subzero
This method tests only whether sys.stdin has data that are immediately available. And I don't think that is strictly guaranteed even when it is connected to a file (e.g. what if it is connected to a file on an NFS and the underlying network connection is spotty?). select is meant for multiplexing, i.e. choosing whatever is ready to read/write from multiple sources/destinations.Reena
This solution works. I had a requirement that the script calls input("prompt>") at some point. But if the user has already entered the text and pressed enter, input will consume the data, but will not add a newline. and the next print will be on the same line as the prompt>. With the select.select, I can print a newline from the code.Willi
C
4

Depending on the goal here:

import fileinput
for line in fileinput.input():
    do_something(line)

can also be useful.

Cowie answered 22/9, 2010 at 2:59 Comment(2)
AFAIK,the fileinput has a disadvantge which limited the args must the filename,but my args may be some control command,Prescriptible
Fair enough! I was trying to handle the common idiom of "do something to a bunch of lines, possibly from stdin" :)Cowie
F
3

As mentioned by others, there's no foolproof way to know if data will become available from stdin, because UNIX doesn't allow it (and more generally because it can't guess the future behavior of whatever program stdin connects to).

Always wait for stdin, even if there may be nothing (that's what grep etc. do), or ask the user for a - argument.

Feet answered 6/6, 2016 at 18:39 Comment(9)
So, you're answering a question by referring to the very same question? WTF??Mettah
@Mettah This is indeed very confusing, haha. It would seem that another question was merged here. I have edited.Feet
What would the process be - wait for stdin and read until an EOF, then process the data? That is, how do grep et al. know when to process data and when to quit?Floater
@Floater the upstream data source is responsible for sending EOF. e.g. printf "" | cat or while read l; do true; done; echo finishing (in the second case you have to input EOF/^D manually to go through)Feet
p.s. Note that if you enter just e.g. cat on the command line, it will wait for input indefinitely. In the above example printf sends EOFFeet
Did you mean to say "because Windows doesn't allow it?" (Unix certainly does allow it)Larkins
@JeremyFriesner Content may only become available after a while.Feet
@Feet certainly -- however, under Unix you can use select() (or poll() or etc) to find out if there is data to read on stdin. You can do it either as an instantaneous-poll (by setting the timeout parameter to 0), or you can have select() block until data is ready, and then return. On Unix-based OS's, STDIN_FILENO behaves more or less the same as any other socket or file descriptor (whereas on Windows, you're mostly out-of-luck -- your only option is to work-around the problem by spawning a separate thread to read from stdin and pass the data back to the main thread via a socket)Larkins
@JeremyFriesner Actually, I think this is another byproduct of the merging mentioned in another comment. The context for my answer has gone missing; I'll try to clarify it. We agree that UNIX lets you know if there is anything in stdin at the moment, and if stdin is connected to a terminal.Feet
S
0

I found a way to use a future from concurrent.futures library. This is a gui program that will allow you to scroll and hit the wait button when you are still waiting for STDIN. Whenever the program sees it has a line of information from STDIN then it will print it in the multi-line box. But it does not lock the script lock out the user from the gui interface why it is checking stdin for a new line. (Please note I tested this in python 3.10.5)

import concurrent.futures
import PySimpleGUI as sg
import sys

def readSTDIN():
    return sys.stdin.readline()

window = sg.Window("Test Window", [[sg.Multiline('', key="MULTI", size=(40, 10))],
                                   [sg.B("QUIT", bind_return_key=True, focus=True),
                                    sg.B("Wait")]], finalize=True, keep_on_top=True)
for arg in sys.argv[1:]:
    window["MULTI"].print(f"ARG={arg}")
with concurrent.futures.ThreadPoolExecutor() as pool:
    futureResult = pool.submit(readSTDIN)
    while True:
        event, values = window.read(timeout=500)
        if event != "__TIMEOUT__":
            window['MULTI'].print(f"Received event:{event}")
        if event in ('QUIT', sg.WIN_CLOSED):
            print("Quit was pressed")
            window.close()
            break
        if futureResult.done(): # flag that the "future" task has a value ready;
                                #therefore process the line from STDIN
            x = futureResult.result()
            window["MULTI"].print(f"STDIN:{x}")
            futureResult = pool.submit(readSTDIN) #add a future for next line of STDIN
Selfdeception answered 26/6, 2022 at 1:40 Comment(0)
B
-1

Saw a lot of answers above, in which almost all the answers required either sys, select, os, etc. However, I got a very simple idea about how continuously to take input from stdin if it has data. We can use try, except block to do so. as,

while(1):
  try:
    inp = input()
  except:
     break

The above loop will run and we are checking for input continuously and storing the input in variable "inp", if there is no input in the stdin then try will throw an error however in except block break statement is present, so the while loop will be terminated

Breslau answered 16/3, 2021 at 5:23 Comment(1)
But the program will wait for input and pause execution. You can't do something based on if stdin is empty.Camorra
C
-2

Using built in modules this can be achieved with following code as Gregg gave already the idea:

import fileinput
isStdin = True
for line in fileinput.input:
    # check if it is not stdin
    if not fileinput.isstdin():
        isStdin = False
        break
    # continue to read stdin
    print(line)
fileinput.close()
Contentious answered 15/10, 2019 at 10:58 Comment(1)
fileinput.input is a function that not iterable, and this will block forever when no dataSelfabuse

© 2022 - 2024 — McMap. All rights reserved.