mlzboy
mlzboy

Reputation: 14691

How do I check if stdin has some data?

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?

Upvotes: 92

Views: 76293

Answers (7)

RAllenAZ
RAllenAZ

Reputation: 46

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

Upvotes: 0

Hüseyin
Hüseyin

Reputation: 27

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

Upvotes: -2

n.caillou
n.caillou

Reputation: 1342

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.

Upvotes: 3

Rakis
Rakis

Reputation: 7864

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.

Upvotes: 96

soraku02
soraku02

Reputation: 340

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

Upvotes: -2

Aaron
Aaron

Reputation: 2521

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.

Upvotes: 100

Gregg Lind
Gregg Lind

Reputation: 21218

Depending on the goal here:

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

can also be useful.

Upvotes: 4

Related Questions