views:

38

answers:

1

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 module 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?

+2  A: 

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)
Aaron Gallagher