Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #47790 > unrolled thread
| Started by | Adam Mercer <ramercer@gmail.com> |
|---|---|
| First post | 2013-06-12 07:15 -0500 |
| Last post | 2013-06-12 07:15 -0500 |
| 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: ValueError: I/O operation on closed file. with python3 Adam Mercer <ramercer@gmail.com> - 2013-06-12 07:15 -0500
| From | Adam Mercer <ramercer@gmail.com> |
|---|---|
| Date | 2013-06-12 07:15 -0500 |
| Subject | Re: ValueError: I/O operation on closed file. with python3 |
| Message-ID | <mailman.3065.1371039350.3114.python-list@python.org> |
On Wed, Jun 12, 2013 at 2:26 AM, Peter Otten <__peter__@web.de> wrote:
> Applying these findings to your script:
>
> from contextlib import contextmanager
> try:
> # python-2.x
> from urllib2 import urlopen
> from ConfigParser import ConfigParser
>
> @contextmanager
> def my_urlopen(url):
> yield urlopen(url).fp
>
> except ImportError:
> # python-3.x
> from urllib.request import urlopen
> from configparser import ConfigParser
> import io
>
> @contextmanager
> def my_urlopen(url):
> resp = urlopen(url)
> yield io.TextIOWrapper(resp.fp)
>
> server='http://www.lsc-group.phys.uwm.edu/~ram/files'
>
> cp = ConfigParser()
> with my_urlopen('%s/latest.ini' % server) as fp:
> cp.readfp(fp)
>
> print(cp.get('version', '10.8'))
>
> I've run it with 2.6, 2.7, 3.2, and 3.3.
Thanks that's very helpful, I hadn't realised that .readfp() had been
deprecated.
Cheers
Adam
Back to top | Article view | comp.lang.python
csiph-web