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

Path csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed2.news.xs4all.nl!xs4all!post.news.xs4all.nl!not-for-mail
Return-Path <ramercer@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.021
X-Spam-Evidence '*H*': 0.96; '*S*': 0.00; 'importerror:': 0.07; 'urllib2': 0.07; 'resp': 0.09; 'try:': 0.09; 'def': 0.12; 'cheers': 0.12; 'adam': 0.16; 'deprecated.': 0.16; 'fp:': 0.16; 'subject:closed': 0.16; 'subject:python3': 0.16; 'wrote:': 0.18; 'wed,': 0.18; 'import': 0.22; "i've": 0.25; 'subject:/': 0.26; 'url:edu': 0.26; 'header:In-Reply-To:1': 0.27; 'am,': 0.29; 'skip:@ 10': 0.30; 'message-id:@mail.gmail.com': 0.30; 'run': 0.32; 'subject:with': 0.35; 'received:209.85': 0.35; 'except': 0.35; 'received:google.com': 0.35; 'yield': 0.36; 'thanks': 0.36; 'received:209': 0.37; 'to:addr:python-list': 0.38; '12,': 0.39; 'to:addr:python.org': 0.39; 'skip:p 20': 0.39; 'skip:u 10': 0.60; 'subject:. ': 0.67; 'applying': 0.72; 'configparser': 0.84; 'otten': 0.84; 'findings': 0.91; '2013': 0.98
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-type; bh=j90toGa+sSkI5ix2nQYg9XNz8EawcmKBdONZH6JPO3Y=; b=syLPow6ry25SU/04QR1cRniUekCEiSMBnpaTL2SKuYJeCu6ML8MWzaMYzCCVqHUrme yTmBODfbJ4PoRH+uytiDlN09kPX9IBynS9ycOKYZTs0WX1OrYHi7TRq6zKm6VTAa/S/K Nwp0172QcJUCNh6BDHvBrLBu7v/s8gDfM/nIcIEJbXN+xhNs5FvDRO3Ccr6twC2wGODW xP6H+/3t+qFtm75uwbaTZLZpQLk5OtpDunTqeqjlnbkDExqZO1jK0pQFckfPMSe/VQZu XcMKAVc81r6BrZukYh0kny1KtKbLNeAt489mXa1kSI7kaVRjpTl/Ocn+6vcrLcvDOvkH lZ9Q==
X-Received by 10.229.148.204 with SMTP id q12mr7430108qcv.32.1371039342544; Wed, 12 Jun 2013 05:15:42 -0700 (PDT)
MIME-Version 1.0
In-Reply-To <kp97qp$v49$1@ger.gmane.org>
References <CA+mfgz1-ywaOLRSV4LBd1fzCvkgHUwE6kLdNPUpiLZzVJX11mA@mail.gmail.com> <kp97qp$v49$1@ger.gmane.org>
From Adam Mercer <ramercer@gmail.com>
Date Wed, 12 Jun 2013 07:15:02 -0500
Subject Re: ValueError: I/O operation on closed file. with python3
To python-list@python.org
Content-Type text/plain; charset=ISO-8859-1
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.3065.1371039350.3114.python-list@python.org> (permalink)
Lines 41
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1371039350 news.xs4all.nl 15914 [2001:888:2000:d::a6]:47672
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:47790

Show key headers only | 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