Frage

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:
        break
    else:
        buffer.join(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.

Keine korrekte Lösung

Andere Tipps

may be you can reading only 1 character at a time using a sys.stdin.read(max) , than read a line..

while True:
    rcvdata = sys.stdin.read(1)
    if len(rcvdata) == 0:
        break

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

and

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

UPDATE

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.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top