How to avoid handling empty stdin using python?

sys.stdin.readline() waiting for the return of EOF (or a new line), so if I have console input, readline() waiting for user input. Instead, I want to print the help and exit with an error if nothing is processed, and not wait for user input.

Reason: I want to write a python program with command line behavior similar to grep .

Test cases:

No input and nothing sent, print help

 $ argparse.py argparse.py - prints arguments echo $? # UNIX echo %ERRORLEVEL% # WINDOWS 2 

Parsing command line arguments

 $ argparse.py abc 0 a 1 b 2 c 

Receive commands with channels

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

List of parseargs.py:

 # $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) 
+4
source share
3 answers

grep may work as if it has one optional argument: a pattern. for instance

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

despite the fact that there was an infinite input to stdin, grep did not receive the required argument and therefore complained.

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

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

Script call

 c:\py_exp>peek_stdin.py < peek_stdin.py True c:\py_exp>peek_stdin.py False 
+2
source

You can check the 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() 

How about this:

 #!/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 check if data is available

 :~> ./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'] 
0
source

Source: https://habr.com/ru/post/1443036/


All Articles