Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #100792

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

From eryk sun <eryksun@gmail.com>
Newsgroups comp.lang.python
Subject Re: What interface is a ‘Popen.stdout’ presenting?
Date 2015-12-23 22:30 -0600
Message-ID <mailman.101.1450931445.2237.python-list@python.org> (permalink)
References <85d1twzlj2.fsf@benfinney.id.au> <858u4kzkyp.fsf@benfinney.id.au>

Show all headers | View raw


On Wed, Dec 23, 2015 at 7:36 PM, Ben Finney <ben+python@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'

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: What interface is a ‘Popen.stdout’ presenting? eryk sun <eryksun@gmail.com> - 2015-12-23 22:30 -0600

csiph-web