Reading pipes in Python

Jeroen van der Ham jeroen_ at _os3.dot.nl
Sat Dec 6 10:54:08 EST 2003


Hello,

During my exploration of Python and rewriting something from Perl to
Python I ran into something odd. I want to use a pipe so that a log of
a program goes into a Python script instead of a log file.

In Perl you just open a file, but specify that it is actually a pipe,
but you read from it the same way.
So I thought I'd do that with the following Python code:

f=open('/tmp/myfifo','r')
while 1:
    print f.readline()

Now, here's the weird thing:
- On Darwin this seems to work okay (seen it work odd on netmounts
tho, but not able to test that atm)
- On Linux this works fine until you send something to the pipe, after
which it prints it and starts printing blank lines over and over.

Even when using os.mkfifo() to create the pipe, this still does not
work properly on Linux.
Anyone any idea why this isn't working and what can be done to make it
work?

Regards, Jeroen.





More information about the Python-list mailing list