By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,939 Members | 1,559 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,939 IT Pros & Developers. It's quick & easy.

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

P: n/a
Hi!

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.

Scott

Jul 18 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
On Fri, 07 Nov 2003 20:39:49 -0800, Scott Chapman wrote:
Hi!

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.

Scott


I may be missing the point here, so forgive me if I am.

However, files can be opened as objects and read and written to very
simply in python.

To open a file:

file_object = open('/path/to/file', 'r')

Then you can read it like thus:

file_contents = file_object.read()

or get each line in a list like this:

file_line_list = file_object.readlines()

you can also write to a file. You have to create the file object first:

save_file = open('filename', 'w')

then you can write to it like:

print >> save_file, 'some text'

you should also close the file objects you open once you're done with them.

Gook luck

Matt
Jul 18 '05 #2

P: n/a
On Fri, 7 Nov 2003 20:39:49 -0800, Scott Chapman
<sc********@mischko.com> wrote:
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.


This can be done - indeed its how the ClearCase config management
tool works on *nix - but it involves some low level magic. I
believe you need to swap out the read/write system calls. I don't
pretend to know how you do that but it can be done. Whether you
could do it in pure Python I doubt, but a C program that called
Python maybe.

Alan G.
Author of the Learn to Program website
http://www.freenetpages.co.uk/hp/alan.gauld
Jul 18 '05 #3

P: n/a
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.


"named pipes", aka FIFOs (created by the mkfifo shell command) are
going to be the "files" you use for that. But, yes, SOME daemon must
be watching on them, e.g. with a select -- could be a generic one that
will spawn the specific one at need, but it's simpler to have the real
script watch on them -- I do agree there's probably no way to get the
kernel to do it for you.
Alex

Jul 18 '05 #4

P: n/a
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
Jul 18 '05 #5

P: n/a
On Saturday 08 November 2003 02:01, Mathias Waack wrote:
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.


I understand named pipes to be "one-way" only. Is that correct?

Scott

Jul 18 '05 #6

P: n/a
Scott Chapman wrote:
I understand named pipes to be "one-way" only. Is that correct?


Not for Linux. Linux named pipes are bidirectional. I assume this
holds for all unices but I'm not sure.

Mathias
Jul 18 '05 #7

P: n/a
In article <2k************@valpo.de>, Mathias Waack <M.*****@gmx.de> wrote:
Scott Chapman wrote:
I understand named pipes to be "one-way" only. Is that correct?


Not for Linux. Linux named pipes are bidirectional. I assume this
holds for all unices but I'm not sure.


No, it doesn't hold everywhere. A more portable bi-directional
construct would be a UNIX/LOCAL domain socket bound to a file.

Gary Duzan
BBN Technologies
A Verizon Company
Jul 18 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.