tMC tMC - 11 months ago 112
Python Question

Python readline from pipe on Linux

When creating a pipe with

it returns 2 file numbers; a read end and a write end which can be written to and read form with
; 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?

Answer Source

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()