tMC tMC - 1 month ago 17
Python Question

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?

Answer

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