2

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.

NoTrueScotsman
  • 305
  • 1
  • 3
  • 10
  • 1
    Are you sure you want `buffer.join(line)`? `buffer` starts as an empty string and will always end the loop as an empty string, so you will always print an empty string. As your code stands, why use the while-loop at all? – Justin O Barber Mar 04 '14 at 03:23

2 Answers2

0

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.

falsetru
  • 357,413
  • 63
  • 732
  • 636
  • My bad; I clarified the context a bit more. I am not allowed to manually stop the program and the laptop is otherwise closed, though still running. – NoTrueScotsman Mar 04 '14 at 03:20
  • how did you do that "ctrl+d"? If I have to write that here I just do `Ctrl+D`. Bland. Tell me tell me tell me. – Guy Mar 04 '14 at 03:20
  • @Sabyasachi, Hold `Ctrl` key and press `D` key. – falsetru Mar 04 '14 at 03:22
  • @falsetru No I mean how did you put those fancy ctrl and d symbols that look like real keys? – Guy Mar 04 '14 at 03:23
  • @NoTrueScotsman, Some scanner output special key when they scan special barcode. Check your scanner manual, and find for `EOF` or similar thing. – falsetru Mar 04 '14 at 03:23
  • 2
    @Sabyasachi, Use `...` – falsetru Mar 04 '14 at 03:24
  • @falsetru oh wow. I never could figure out what that tag was supposed to do. thanks. :D – Guy Mar 04 '14 at 03:25
  • @NoTrueScotsman, The scanner (Symbol LS2208) I used in previous work allow to configure the scan to send customized keysequence. Check your own if it is possible. – falsetru Mar 04 '14 at 03:56
0

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'

Community
  • 1
  • 1
durga
  • 404
  • 6
  • 12