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


Groups > comp.lang.python > #87022

Re: io.open vs. codecs.open

Path csiph.com!usenet.pasdenom.info!news.redatomik.org!newsfeed.xs4all.nl!newsfeed3.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <fomcl@yahoo.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.001
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'interfaces': 0.04; 'encoding': 0.05; 'interpreter': 0.05; 'string.': 0.05; '(python': 0.07; '*not*': 0.07; 'binary': 0.07; 'friend.': 0.07; 'wednesday,': 0.07; 'string': 0.09; 'bytes.': 0.09; 'strings.': 0.09; 'python': 0.11; '2.7': 0.14; 'mostly': 0.14; '(before': 0.16; '(use': 0.16; 'argument,': 0.16; 'backwards': 0.16; 'mode,': 0.16; "mode='r',": 0.16; 'open()': 0.16; 'open(file,': 0.16; 'porting': 0.16; 'read()': 0.16; 'streams.': 0.16; 'to:addr:pearwood.info': 0.16; 'to:addr:steve+comp.lang.python': 0.16; "to:name:steven d'aprano": 0.16; 'wrote:': 0.18; 'module': 0.19; 'basically': 0.19; 'thanks.': 0.20; 'proposed': 0.22; 'to:name:python-list@python.org': 0.22; 'enhanced': 0.24; 'simpler': 0.24; 'unicode': 0.24; 'versions': 0.24; 'pass': 0.26; 'header:In-Reply-To:1': 0.27; 'to:2**1': 0.27; "doesn't": 0.30; 'mode': 0.30; 'url:mailman': 0.30; '3.x': 0.31; "d'aprano": 0.31; 'steven': 0.31; 'file': 0.32; 'supposed': 0.32; 'interface': 0.32; 'quite': 0.32; 'text': 0.33; 'open': 0.33; 'url:python': 0.33; '-----': 0.33; 'something': 0.35; 'but': 0.35; 'there': 0.35; 'version': 0.36; 'really': 0.36; 'object,': 0.36; 'url:listinfo': 0.36; "didn't": 0.36; 'method': 0.36; 'hi,': 0.36; 'url:org': 0.36; 'so,': 0.37; 'email addr:python.org': 0.37; 'url:library': 0.38; 'to:addr:python-list': 0.38; 'files': 0.38; 'recent': 0.39; 'explain': 0.39; 'does': 0.39; 'subject:': 0.39; 'realize': 0.39; 'to:addr:python.org': 0.39; 'url:mail': 0.40; 'days': 0.60; 'received:67.195': 0.60; 'received:98.137': 0.60; 'new': 0.61; 'march': 0.61; 're:': 0.63; 'skip:n 10': 0.64; 'email name:python- list': 0.65; 'subject:. ': 0.67; 'between': 0.67; 'header:Reply- To:1': 0.67; 'believe': 0.68; 'detail.': 0.68; 'received:gq1.yahoo.com': 0.68; 'received:mail.gq1.yahoo.com': 0.68; 'default': 0.69; 'safe': 0.72; 'discovered': 0.83; '2015': 0.84; 'received:bullet.mail.gq1.yahoo.com': 0.84; 'opens': 0.91; 'differences': 0.93
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=yahoo.com; s=s2048; t=1425649913; bh=0mRVQLFySh6fIEwD61pgt8foWMnVDWsYLL5f9kbUxlY=; h=Date:From:Reply-To:To:In-Reply-To:References:Subject:From:Subject; b=op5lu2dwHgmT+Ys8dR8cAXXaDvrS1BmE6KxanAhxUuZaItmrQ5RTFbqfQ7JJowBCBfdwrJw4VT4Ym8za/o2rE8pnT0f5oNKoOwrgLhf1WNGydbozFdR17hyUTNy8izWhYHYvB1ZJdIfnI8o3NbqhCPCUIR7ttDUirzhHCrB94D18RkDuEPXjAt1gYiFZl+ahLhAwkJCPuuKDgXY7tBIQzTw5CrqQwWajIlHEQnLmFdImQDCS33/MSO5HuIvhbbBIajF/EGFr0n36k0o5SdwQDUnEMU0tHJRZpOYFJeLMCOY0pGtb7wzufI47rUI99kj7dpar/VCE5mw3hh45xWdRgg==
X-Yahoo-Newman-Property ymail-4
X-Yahoo-Newman-Id 652512.41863.bm@omp1007.mail.gq1.yahoo.com
X-YMail-OSG tRvEa0wVM1mgH27V5lmB4xDnO_MqmAMqFmsQEtvRotOxSJfc10EW4524ClzSdRc GEuUh4c4nC2fgVv_oxVorg4bJcJAFTm17HmMmzEYq9poYDB06om5gmGtFdFS3Q9qRbHXEsSRHuI4 7o1OyXv79ddhmTv8uJOJe3hdSWOW_2sPWM6fXucO72qNznuFo05DSIVYwiAfg5p2tebRi.l27yZk KtUiapGA5aULR.3wkAL4n7TOxlCu8XzFM9leoZo1eetTNPwWV8zSHozgv2efssEPDa2E3893N7C3 2EA5aZTMM.2uQsFjunNgc3vG5zt.wk6i8gmohoKi26EDxbH9lhg0RJUBDzcBMsFJ0nn9j.F3N3r5 oHU7ZNSXLeLWYe1FD6wXaOnCViwEJ5h7jB8qjS4rTJAHFkYDu_YmqXeelVAaUmcQR6hU3Mpc.4Jg Nto5tOD7vV0J0ADM9kagY9qK1VzOeZAP.aT_RZ168uMtWfT3r3FX9E75012a_RQ58vlMDY7LTmnb iX5mOMjDvIsBRb34-
Date Fri, 6 Mar 2015 13:48:56 +0000 (UTC)
From Albert-Jan Roskam <fomcl@yahoo.com>
Reply-To Albert-Jan Roskam <fomcl@yahoo.com>
To Steven D'Aprano <steve+comp.lang.python@pearwood.info>, "python-list@python.org" <python-list@python.org>
In-Reply-To <54f76359$0$13012$c3e8da3$5496439d@news.astraweb.com>
References <54f76359$0$13012$c3e8da3$5496439d@news.astraweb.com>
Subject Re: io.open vs. codecs.open
MIME-Version 1.0
Content-Type text/plain; charset=UTF-8
Content-Transfer-Encoding 7bit
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.19
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <https://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 <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.110.1425650043.21433.python-list@python.org> (permalink)
Lines 100
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1425650043 news.xs4all.nl 2926 [2001:888:2000:d::a6]:33866
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:87022

Show key headers only | View raw



----- Original Message -----

> From: Steven D'Aprano <steve+comp.lang.python@pearwood.info>
> To: python-list@python.org
> Cc: 
> Sent: Wednesday, March 4, 2015 8:56 PM
> Subject: Re: io.open vs. codecs.open
> 
> Albert-Jan Roskam wrote:
> 
>>  Hi,
>> 
>>  Is there a (use case) difference between codecs.open and io.open? What is
>>  the difference? A small difference that I just discovered is that
>>  codecs.open(somefile).read() returns a bytestring if no encoding is
>>  specified*), but a unicode string if an encoding is specified. io.open
>>  always returns a unicode string.
> 
> What version of Python are you using?


Python 2.7 and 3.4.


> In Python 3, io.open is used as the built-in open. I believe this is
> guaranteed, and not just an implementation detail.
> 
> The signatures and capabilities are quite different:
> 
> codecs.open:
> 
> open(filename, mode='rb', encoding=None, errors='strict', 
> buffering=1)
> 
> io.open:
> 
> open(file, mode='r', buffering=-1, encoding=None,

>          errors=None, newline=None, closefd=True, opener=None)

Thanks. I didn't realize that closefd was also available in Python 2. I had only seen it in Python 3 open()

> 
> io.open does *not* always produce Unicode strings. If you pass 'rb' as 
> the
> mode, the file is opened in binary mode, not text mode, and the read()
> method will return bytes.



So, in recent versions of Python 2 (Python 2.7.x, 2.6) I can basically ditch codecs.open() and the standard open()?
Given that standard open() has no encoding parameter, it is only really safe for use with binary data (binary mode).


> As usual, help() in the interactive interpreter is your friend.
> help(codecs.open) and help(io.open) will explain the many differences
> between them, including that codecs.open always opens the file in binary
> mode.
> 
> As for use-cases, I think that codecs.open is mostly a left-over from the
> Python 2 days when the built-in open had a much simpler interface and fewer
> capabilities. In Python 2, built-in open doesn't take an encoding argument,
> so if you want to use something other than binary mode or the default
> encoding, you were supposed to use codecs.open.
> 
> In Python 2.6, the io module was added to Python 2 to aid in porting to
> Python 3. The docs say:
> 
>     New in version 2.6.
> 
>     The io module provides the Python interfaces to stream handling.
>     Under Python 2.x, this is proposed as an alternative to the
>     built-in file object, but in Python 3.x it is the default
>     interface to access files and streams.
> 
> https://docs.python.org/2/library/io.html
> 
> 
> To summarise:
> 
> * In Python 2, if you want to supply an encoding to open, use codecs.open
> (before 2.6) or io.open (2.6 and later);
> 
> * If you want the enhanced capabilities of Python 3 open, use io.open;
> 
> * In Python 3, io.open is the same thing as built-in open;
> 
> * And codecs.open is (I think) mostly there for backwards compatibility.
> 
> 
> 
> 
> -- 
> Steven
> 
> -- 
> https://mail.python.org/mailman/listinfo/python-list
> 

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


Thread

io.open vs. codecs.open Albert-Jan Roskam <fomcl@yahoo.com> - 2015-03-04 04:12 -0800
  Re: io.open vs. codecs.open Steven D'Aprano <steve+comp.lang.python@pearwood.info> - 2015-03-05 06:56 +1100
    Re: io.open vs. codecs.open Mark Lawrence <breamoreboy@yahoo.co.uk> - 2015-03-04 20:15 +0000
    Re: io.open vs. codecs.open Albert-Jan Roskam <fomcl@yahoo.com> - 2015-03-06 13:48 +0000

csiph-web