Can I hook a "file" to a python script?

Mathias Waack M.Waack at gmx.de
Sat Nov 8 05:01:39 EST 2003


Scott Chapman wrote:
> I'd like a "file" on the Linux box to actually be the input and
> output
> of a Python script.  Anything written to the file would be sent to
> a database and anything read from the file would come from the
> database. I know how to do the database end of this but I'm not
> sure if a script
> can be hooked to a "file" transparently.  The script would probably
> have to be run as a daemon.  I doubt there's enough magic in the
> Linux world to make it so that a read/write would actually launch
> the script. That'd be Ok.

The solution is very easy. There are at least two ways. First the
easier one: create a named pipe using mkfifo(1) and start your
python script which should attach itself to one end of the pipe. The
second solution is bit more complex: you need a new device. The
device driver will only forward all input to your user space daemon
or read all output from it. This way your python script can be
started automagically each time someone accesses the device special
file. 

And I'm sure there is a whole bunch of other solutions...

Mathias




More information about the Python-list mailing list