Linux上管道上的Python readline [英] Python readline from pipe on Linux

查看:117
本文介绍了Linux上管道上的Python readline的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用os.pipe()创建管道时,它将返回2个文件编号;可以用os.write()/os.read()进行读写的读取端和写入端;没有os.readline().是否可以使用readline?

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

简而言之,当您仅拥有文件句柄编号时,是否可以使用readline?

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

推荐答案

您可以使用

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

这篇关于Linux上管道上的Python readline的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆