I'm working on some Python code for a robot. This code utilizes barcode input from a scanner and uses regex to filter the appropriate data. The trouble is, the input is pulled in from the command line. It pulls the input fine, but doesn't close itself when there's no more input.

buffer = ""

while True:
    line = sys.stdin.readline().rstrip()
    if not line:

print buffer

Any help would be much appreciated.

Edit: I should mention that this is on Linux and that the laptop that this is running on is closed, and I am not allowed to manually stop the program.

No correct solution


may be you can reading only 1 character at a time using a , than read a line..

while True:
    rcvdata =
    if len(rcvdata) == 0:

also, check this thread out from SOF :Python blocks until max is read (if max>=0), blocks until EOF else, but select indicates there is data to be read


sys.stdin.readline() reads without prompt, returning 'nothing in between'

Press EOF Key sequence: press ctrl + D to denote the end of the input (in unix, osx). If you use Windows, press ctrl + Z.

Otherwise the program will not return from sys.stdin.readline().


If you can't access keyboard, use scanner's feature; Some scanners allow you to send pre-defined sequence when they scan specific barcode. Some also allow you define your own key sequence. Find scanner manual if this is possible.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top