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

Adam Mercer ramercer at gmail.com
Wed Jun 12 08:15:02 EDT 2013


On Wed, Jun 12, 2013 at 2:26 AM, Peter Otten <__peter__ at 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



More information about the Python-list mailing list