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


Groups > comp.lang.python > #86915

Re: io.open vs. codecs.open

Path csiph.com!usenet.pasdenom.info!news.redatomik.org!newsfeed.xs4all.nl!newsfeed4.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; 'from:addr:yahoo.co.uk': 0.04; 'interfaces': 0.04; 'encoding': 0.05; 'interpreter': 0.05; 'string.': 0.05; '*not*': 0.07; 'binary': 0.07; 'friend.': 0.07; 'string': 0.09; 'bytes.': 0.09; 'lawrence': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'strings.': 0.09; 'python': 0.11; 'language.': 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(file,': 0.16; 'porting': 0.16; 'read()': 0.16; 'received:80.91.229.3': 0.16; 'received:plane.gmane.org': 0.16; 'streams.': 0.16; 'language': 0.16; 'wrote:': 0.18; 'module': 0.19; 'proposed': 0.22; 'header :User-Agent:1': 0.23; 'enhanced': 0.24; 'simpler': 0.24; 'unicode': 0.24; 'pass': 0.26; 'header:X-Complaints-To:1': 0.27; 'header:In-Reply-To:1': 0.27; 'url:bugs': 0.29; "doesn't": 0.30; 'mode': 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; 'something': 0.35; 'but': 0.35; 'there': 0.35; 'version': 0.36; 'object,': 0.36; 'method': 0.36; 'hi,': 0.36; 'url:org': 0.36; 'url:library': 0.38; 'to:addr:python-list': 0.38; 'files': 0.38; 'explain': 0.39; 'does': 0.39; 'to:addr:python.org': 0.39; 'received:org': 0.40; 'days': 0.60; 'new': 0.61; 'skip:n 10': 0.64; 'our': 0.64; 'charset:windows-1252': 0.65; 'subject:. ': 0.67; 'between': 0.67; 'believe': 0.68; 'detail.': 0.68; 'default': 0.69; 'discovered': 0.83; 'opens': 0.91; 'differences': 0.93
X-Injected-Via-Gmane http://gmane.org/
To python-list@python.org
From Mark Lawrence <breamoreboy@yahoo.co.uk>
Subject Re: io.open vs. codecs.open
Date Wed, 04 Mar 2015 20:15:59 +0000
References <mailman.37.1425471638.21433.python-list@python.org> <54f76359$0$13012$c3e8da3$5496439d@news.astraweb.com>
Mime-Version 1.0
Content-Type text/plain; charset=windows-1252; format=flowed
Content-Transfer-Encoding 7bit
X-Gmane-NNTP-Posting-Host host-78-147-31-244.as13285.net
User-Agent Mozilla/5.0 (Windows NT 6.3; WOW64; rv:31.0) Gecko/20100101 Thunderbird/31.5.0
In-Reply-To <54f76359$0$13012$c3e8da3$5496439d@news.astraweb.com>
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.45.1425500171.21433.python-list@python.org> (permalink)
Lines 75
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1425500171 news.xs4all.nl 2887 [2001:888:2000:d::a6]:40455
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:86915

Show key headers only | View raw


On 04/03/2015 19:56, Steven D'Aprano wrote:
> 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?
>
> 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)
>
> 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.
>
> 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.
>

See http://bugs.python.org/issue8796 "Deprecate codecs.open()".

-- 
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.

Mark Lawrence

Back to comp.lang.python | Previous | NextPrevious in thread | Next 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