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


Groups > comp.lang.python > #47790

Re: ValueError: I/O operation on closed file. with python3

References <CA+mfgz1-ywaOLRSV4LBd1fzCvkgHUwE6kLdNPUpiLZzVJX11mA@mail.gmail.com> <kp97qp$v49$1@ger.gmane.org>
From Adam Mercer <ramercer@gmail.com>
Date 2013-06-12 07:15 -0500
Subject Re: ValueError: I/O operation on closed file. with python3
Newsgroups comp.lang.python
Message-ID <mailman.3065.1371039350.3114.python-list@python.org> (permalink)

Show all headers | View raw


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 comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: ValueError: I/O operation on closed file. with python3 Adam Mercer <ramercer@gmail.com> - 2013-06-12 07:15 -0500

csiph-web