473,473 Members | 1,583 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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 3784
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: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.