Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python readline from pipe on Linux

When creating a pipe with os.pipe() it returns 2 file numbers; a read end and a write end which can be written to and read form with os.write()/os.read(); there is no os.readline(). Is it possible to use readline?

import os
readEnd, writeEnd = os.pipe()
# something somewhere writes to the pipe
firstLine = readEnd.readline() #doesn't work; os.pipe returns just fd numbers

In short, is it possible to use readline when all you have is the file handle number?

like image 880
tMC Avatar asked May 31 '11 21:05

tMC


1 Answers

You can use os.fdopen() to get a file-like object from a file descriptor.

import os
readEnd, writeEnd = os.pipe()
readFile = os.fdopen(readEnd)
firstLine = readFile.readline()
like image 84
bradley.ayers Avatar answered Sep 22 '22 13:09

bradley.ayers