开发者

What is the subprocess equivalent of passing "b" to os.popen2?

In Python 2.x, os.popen(command, "b") gives me a binary stream of the given command's output. This is primarily important on Windows, where binary and text streams actually give you different bytes.

The subprocess mod开发者_开发知识库ule is supposed to replace os.popen and the other child-process spawning APIs. However, the conversion docs don't talk about dealing with the "b" mode at all. How do you get a binary output stream using subprocess?


It does by default, unless you're doing Popen(..., universal_newlines=True).

class Popen(object):
    [...]
    def __init__(self, ...):
        [...]
        if p2cwrite is not None:
            self.stdin = os.fdopen(p2cwrite, 'wb', bufsize)
        if c2pread is not None:
            if universal_newlines:
                self.stdout = os.fdopen(c2pread, 'rU', bufsize)
            else:
                self.stdout = os.fdopen(c2pread, 'rb', bufsize)
        if errread is not None:
            if universal_newlines:
                self.stderr = os.fdopen(errread, 'rU', bufsize)
            else:
                self.stderr = os.fdopen(errread, 'rb', bufsize)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜