[Tutor] bind line-oriented device output?

Tom Roche Tom_Roche at pobox.com
Mon Mar 29 01:51:23 CEST 2010

I'd like to learn to pythonically redirect the output from a
line-oriented character device to a particular file or process,
regardless of focus, on a generic graphical OS. But I don't want to
redirect stdin entirely. Here's the usecase:

My school has a seminar for which we record attendance by scanning the
student ID card, or rather the barcode on its back, with a handheld
USB scanner. This is pretty straightforward, except that the barcode
scanner is like a keyboard writing the ASCII equivalent of the barcode
(== the student's ID#) to stdin. FWIW, the scanner writes lines: I'm
not sure if EOL is \r, \n, or \r\n, but I suspect the latter.

Since the scanner is connected to an ordinary multiprocessing laptop on
which one will likely be doing other things while scanning (notably
setting up to record the presenter), it sometimes happens (especially
until one learns to pay attention to this) that one writes to a frame
other than the text file into which we want record attendee ID#s. This
semester recurs every {fall, spring}, so someone faces this {pitfall,
learning curve} at regular intervals.

How to prevent writing the wrong target? One trivial solution--shlep a
dedicated USB host for the scanner--is deprecated. An OS-specific
solution (e.g. relying on a linux raw device) is also undesirable: I
use ubuntu, but others will probably use mac or windows.

Rather, It Would Be Nice, and useful for this seminar's mechanics, to
be able to run some code to which one could say, see this device? and
this file? Make the device's output go only to that file. For extra
credit, don't let anything else write that file while this code is

Can python do that? Or does one need to get closer to the metal?

TIA, Tom Roche <Tom_Roche at pobox.com>

More information about the Tutor mailing list