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


Groups > comp.lang.python > #47747

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

Path csiph.com!usenet.pasdenom.info!gegeweb.org!de-l.enfer-du-nord.net!feeder1.enfer-du-nord.net!feeds.phibee-telecom.net!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <python-python-list@m.gmane.org>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.000
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'scripts': 0.03; 'error:': 0.07; 'importerror:': 0.07; 'problem:': 0.07; 'python3': 0.07; 'urllib2': 0.07; 'bytes,': 0.09; 'deprecated': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'resp': 0.09; 'try:': 0.09; 'valueerror:': 0.09; 'runs': 0.10; 'python': 0.11; 'def': 0.12; 'assume': 0.14; 'adam': 0.16; 'arg': 0.16; 'expects': 0.16; 'fp:': 0.16; 'lineno,': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:plane.gmane.org': 0.16; 'received:t-ipconnect.de': 0.16; 'subject:closed': 0.16; 'subject:python3': 0.16; 'tuple': 0.16; 'typeerror:': 0.16; 'wrote:': 0.18; 'trying': 0.19; '>>>': 0.22; 'example': 0.22; 'import': 0.22; 'header:User-Agent:1': 0.23; 'accommodate': 0.24; 'bytes': 0.24; 'skip:l 30': 0.24; 'file.': 0.24; "i've": 0.25; 'script': 0.25; 'skip:" 40': 0.26; 'subject:/': 0.26; 'url:edu': 0.26; 'header:X-Complaints-To:1': 0.27; 'skip:p 30': 0.29; 'skip:@ 10': 0.30; 'skip:( 20': 0.30; "i'm": 0.30; '(which': 0.31; '"",': 0.31; 'file:': 0.31; 'python2.7': 0.31; 'sep': 0.31; 'file': 0.32; 'run': 0.32; 'another': 0.32; 'text': 0.33; 'linux': 0.33; 'running': 0.33; '(most': 0.33; "i'd": 0.34; 'subject:with': 0.35; 'except': 0.35; 'possible.': 0.35; 'skip:u 20': 0.35; 'but': 0.35; 'there': 0.35; '14,': 0.36; 'yield': 0.36; 'starting': 0.37; 'expected': 0.38; 'to:addr:python-list': 0.38; 'issue': 0.38; 'recent': 0.39; 'to:addr:python.org': 0.39; 'skip:p 20': 0.39; 'received:org': 0.40; 'users': 0.40; 'skip:u 10': 0.60; 'read': 0.60; 'first': 0.61; 'more': 0.64; 'subject:. ': 0.67; 'receive': 0.70; 'applying': 0.72; 'configparser': 0.84; 'favour': 0.91; 'findings': 0.91; 'url:latest': 0.91; 'wanting': 0.93
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Peter Otten <__peter__@web.de>
Subject Re: ValueError: I/O operation on closed file. with python3
Date Wed, 12 Jun 2013 09:26:47 +0200
Organization None
References <CA+mfgz1-ywaOLRSV4LBd1fzCvkgHUwE6kLdNPUpiLZzVJX11mA@mail.gmail.com>
Mime-Version 1.0
Content-Type text/plain; charset="ISO-8859-1"
Content-Transfer-Encoding 7Bit
X-Gmane-NNTP-Posting-Host p50849e94.dip0.t-ipconnect.de
User-Agent KNode/4.7.3
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.3052.1371022005.3114.python-list@python.org> (permalink)
Lines 138
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1371022005 news.xs4all.nl 15899 [2001:888:2000:d::a6]:35785
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:47747

Show key headers only | View raw


Adam Mercer wrote:

> Hi
> 
> I'm trying to update one of my scripts so that it runs under python2
> and python3, but I'm running into an issue that the following example
> illustrates:
> 
> $ cat test.py
> try:
>   # python-2.x
>   from urllib2 import urlopen
>   from ConfigParser import ConfigParser
> except ImportError:
>   # python-3.x
>   from urllib.request import urlopen
>   from configparser import ConfigParser
> 
> server='http://www.lsc-group.phys.uwm.edu/~ram/files'
> 
> fp = urlopen('%s/latest.ini' % server).fp
> cp = ConfigParser()
> cp.readfp(fp)
> print(cp.get('version', '10.8'))
> $
> 
> This works as expected when using python2:
> 
> $ python2.7 test.py
> 5.2.10
> $
> 
> but when using python3 I receive the following error:
> 
> $ python3.3 test.py
> Traceback (most recent call last):
>   File "test.py", line 14, in <module>
>     cp.readfp(fp)
>   File
>   
"/opt/local/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/configparser.py",
> line 753, in readfp
>     self.read_file(fp, source=filename)
>   File
>   
"/opt/local/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/configparser.py",
> line 708, in read_file
>     self._read(f, source)
>   File
>   
"/opt/local/Library/Frameworks/Python.framework/Versions/3.3/lib/python3.3/configparser.py",
> line 1010, in _read
>     for lineno, line in enumerate(fp, start=1):
> ValueError: I/O operation on closed file.
> $
> 
> Is there a way to get this working in both python2 and python3?
> 
> This is a small script and I'm starting to have some users wanting to
> use python3 and others sticking to python2 so I'd like to accommodate
> them both if possible.

Python 3.3.0 (v3.3.0:bd8afb90ebf2, Sep 30 2012, 14:49:00) 
[GCC 4.6.1] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> from urllib.request import urlopen
>>> url = "http://www.lsc-group.phys.uwm.edu/~ram/files/latest.ini"
>>> fp = urlopen(url).fp
>>> fp.read()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ValueError: read of closed file
>>> resp = urlopen(url)
>>> resp.fp.read()
b'[version]\n10.5 = 5.2.4\n10.6 = 5.2.10\n10.7 = 5.2.10\n10.8 = 5.2.10\n'

I don't know whether this behaviour is intentional or accidental (I assume 
the latter).

I then ran into another problem:

>>> from configparser import ConfigParser
>>> p = ConfigParser()
>>> resp = urlopen(url)
>>> p.readfp(resp.fp)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.3/configparser.py", line 754, in readfp
    self.read_file(fp, source=filename)
  File "/usr/local/lib/python3.3/configparser.py", line 709, in read_file
    self._read(f, source)
  File "/usr/local/lib/python3.3/configparser.py", line 1011, in _read
    if line.strip().startswith(prefix):
TypeError: startswith first arg must be bytes or a tuple of bytes, not str

So ConfigParser.readfp() (which has been deprecated in favour of 
read_file(), btw.) expects a text file:

>>> from io import TextIOWrapper
>>> resp = urlopen(url)
>>> p.readfp(TextIOWrapper(resp.fp))
>>> p.get("version", "10.8")
'5.2.10'

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. 

Back to comp.lang.python | Previous | Next | Find similar | Unroll thread


Thread

Re: ValueError: I/O operation on closed file. with python3 Peter Otten <__peter__@web.de> - 2013-06-12 09:26 +0200

csiph-web