Reading pipes in Python

Steve nospam at nopes
Mon Dec 15 21:23:08 EST 2003


How do we open a pipe programatically? I'm interested in redirecting 
output from a program to a pipe and then using it from a python script. 
Thanks

Steve

Jeroen van der Ham wrote:
> 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