Bruce Peterson
Bruce Peterson

Reputation: 392

How do I avoid processing an empty stdin with python?

The sys.stdin.readline() waits for an EOF (or new line) before returning, so if I have a console input, readline() waits for user input. Instead I want to print help and exit with an error if there is nothing to process, not wait for user input.

Reason: I'm looking to write a python program with command line behaviour similar to grep.

Test cases:

No input and nothing piped, print help

$ argparse.py
argparse.py - prints arguments

echo $?            # UNIX
echo %ERRORLEVEL%  # WINDOWS
2

Command line args parsed

$ argparse.py a b c 
0 a
1 b
2 c

Accept piped commands

$ ls | argparse.py
0 argparse.py
1 aFile.txt

parseargs.py listing:

# $Id: parseargs.py

import sys
import argparse

# Tried these too:
# import fileinput - blocks on no input
# import subprocess - requires calling program to be known

def usage():
    sys.stderr.write("{} - prints arguments".fomrat(sys.argv[0])
    sys.stderr.flush()
    sys.exit(2)

def print_me(count, msg):
    print '{}: {:>18} {}'.format(count, msg.strip(), map(ord,msg))

if __name__ == '__main__':
    USE_BUFFERED_INPUT = False
    # Case 1: Command line arguments  
    if len(sys.argv) > 1:
        for i, arg in enumerate(sys.argv[1:]):
            print_me( i, arg)
    elif USE_BUFFERED_INPUT:  # Note: Do not use processing buffered inputs  
        for i, arg in enumerate(sys.stdin):
            print_me( i, arg)
    else:
        i=0
        #####  Need to deterime if the sys.stdin is empty.
        #####  if READLINE_EMPTY:
        #####      usage()
        while True:
            arg = sys.stdin.readline() #Blocks if no input
            if not arg:
                break
            print_me( i, arg)
            i += 1
    sys.exit(0)

Upvotes: 6

Views: 4710

Answers (3)

savruk
savruk

Reputation: 545

You may want to check getopt module. Basic example:

import getopt
import sys

def main(argv):
    try:
        opts, args = getopt.getopt(argv, "has:f:") # "has:f:" are the arguments 
    except getopt.GetoptError:
        print "print usage()"
        sys.exit(1)
    if not opts and not args:
        print "print usage()"
        sys.exit(1)

    print "args passed", opts, args
if __name__ == "__main__":
    main(sys.argv[1:])


~> python blabla.py
print usage()
~> python blabla.py -a arg
args passed [('-a', '')] ['arg']
~> python blabla.py -b as  ----> this fails because -b is not defined for getopt at second parameter
print usage()

What about this one:

#!/usr/bin/env python
import getopt
import sys
import select


def main(argv):
    try:
        opts, args = getopt.getopt(argv, "has:f:") # "has:f:" are the arguments
    except getopt.GetoptError:
        print "print usage()"
        sys.exit(1)
    if not opts and not args:
        a, b, c = select.select([sys.stdin], [], [], 0.2)
        if a:
            itera = iter(a[0].readline, "")
            for line in itera:
                data = line.strip()
                print data
        else:
            print "print usage()"

    print "args passed", opts, args
if __name__ == "__main__":
    main(sys.argv[1:])

select.select helps to check if there is data coming

:~> ./hebele.py 
print usage()
args passed [] []

:~> ping www.google.com | ./hebele.py 
PING www.google.com (173.194.67.105) 56(84) bytes of data.
64 bytes from blabla (173.194.67.105): icmp_seq=1 ttl=48 time=16.7 ms
64 bytes from blabla (173.194.67.105): icmp_seq=2 ttl=48 time=17.1 ms
64 bytes from blabla (173.194.67.105): icmp_seq=3 ttl=48 time=17.1 ms
^CTraceback (most recent call last):
  File "./hebele.py", line 25, in <module>
    main(sys.argv[1:])
  File "./hebele.py", line 17, in main
    for line in itera:
KeyboardInterrupt
:~> ls | ./hebele.py 
Aptana_Studio_3
Desktop
...
workspace
args passed [] []

:~> ./hebele.py -a bla
args passed [('-a', '')] ['bla']
:~> ./hebele.py sdfsdf sadf sdf
args passed [] ['sdfsdf', 'sadf', 'sdf']

Upvotes: 0

Joran Beasley
Joran Beasley

Reputation: 113950

import sys,os
print os.fstat(sys.stdin.fileno()).st_size > 0

Calling script

c:\py_exp>peek_stdin.py < peek_stdin.py
True

c:\py_exp>peek_stdin.py
False

Upvotes: 4

msw
msw

Reputation: 43487

grep can work the way it does because it has one non-optional argument: the pattern. For example

$ grep < /dev/zero
Usage: grep [OPTION]... PATTERN [FILE]...
Try `grep --help' for more information.

even though there was infinite input available on stdin, grep didn't get the required argument and therefore complained.

If you want to use only optional arguments and error out if stdin is a terminal, look at file.isatty().

Upvotes: 5

Related Questions