Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #100792 > unrolled thread
| Started by | eryk sun <eryksun@gmail.com> |
|---|---|
| First post | 2015-12-23 22:30 -0600 |
| Last post | 2015-12-23 22:30 -0600 |
| Articles | 1 — 1 participant |
Back to article view | Back to comp.lang.python
This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by
below is the oldest one visible, not the original post.
Re: What interface is a ‘Popen.stdout’ presenting? eryk sun <eryksun@gmail.com> - 2015-12-23 22:30 -0600
| From | eryk sun <eryksun@gmail.com> |
|---|---|
| Date | 2015-12-23 22:30 -0600 |
| Subject | Re: What interface is a ‘Popen.stdout’ presenting? |
| Message-ID | <mailman.101.1450931445.2237.python-list@python.org> |
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 top | Article view | comp.lang.python
csiph-web