Re: What interface is a ‘Popen.stdout’ presenting?

eryk sun eryksun at gmail.com
Wed Dec 23 23:30:02 EST 2015


On Wed, Dec 23, 2015 at 7:36 PM, Ben Finney <ben+python at benfinney.id.au> wrote:
> So how do I get from a Python 2 ‘file’ object, to whatever
> ‘io.TextIOWrapper’ wants?

I would dup the file descriptor and close the original file. Then open
the file descriptor using io.open:

    >>> p = subprocess.Popen(['uname'], stdout=subprocess.PIPE)
    >>> fd = os.dup(p.stdout.fileno())
    >>> fd
    4
    >>> p.stdout.close()
    >>> fout = io.open(fd, 'r')
    >>> fout
    <_io.TextIOWrapper name=4 encoding='UTF-8'>
    >>> fout.read()
    u'Linux\n'



More information about the Python-list mailing list