472,958 Members | 2,273 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,958 software developers and data experts.

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

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
7 3746
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
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
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: leroybt.rm | last post by:
I don't understand why this does not work: <FILE1> test1.py #Import Packages import string # data=0 data=data+1
10
by: mike | last post by:
regards: I use Jtidy (api) to translate a HTML file into a "XHTML file". But The "XHTML file" cannot be identified by nokia 6600. Do I miss something important? Or this is Jtidy's weakness or...
1
by: Mark Sandfox | last post by:
Is there a way to restrict the user to only selecting and sending either a ..gif or .jpg. Everything I have read says this option can not be done by design (security reasons). I find that irronic...
2
by: mark | last post by:
How do I detect that a particular form element is a file upload or if the file upload has worked? In the Python cgi module documentation I found suggested code... form = cgi.FieldStorage()...
1
by: Denton | last post by:
Hi all: I working in MS window environment. Now I want to use a python script to import some data from CSV file to MDB file. but I have some problems about creating MDB file in DAO. I am using...
1
by: Gary Wessle | last post by:
I am getting this error when I try to run the code below **************** f = open("~/m", "r") print f.read() **************** **************** :~$ python python/my.py Traceback (most...
0
by: itzel | last post by:
Hello!! In using tarfile to group thousands of small files from a directory and then compress it. I already compress a group of files in my pc, but I need do it in a server and I'm testing the...
2
by: John Nagle | last post by:
For some reason, Python's parser for "robots.txt" files doesn't like Wikipedia's "robots.txt" file: False The Wikipedia robots.txt file passes robots.txt validation, and it doesn't disallow...
0
by: Timothy Grant | last post by:
On Fri, Aug 29, 2008 at 12:48 AM, Alexis Boutillier <alexis.boutillier@arteris.comwrote: http://docs.python.org/ref/import.html -- Stand Fast, tjg.
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
2
by: giovanniandrean | last post by:
The energy model is structured as follows and uses excel sheets to give input data: 1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
3
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
3
by: nia12 | last post by:
Hi there, I am very new to Access so apologies if any of this is obvious/not clear. I am creating a data collection tool for health care employees to complete. It consists of a number of...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.