Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #86915
| From | Mark Lawrence <breamoreboy@yahoo.co.uk> |
|---|---|
| Subject | Re: io.open vs. codecs.open |
| Date | 2015-03-04 20:15 +0000 |
| References | <mailman.37.1425471638.21433.python-list@python.org> <54f76359$0$13012$c3e8da3$5496439d@news.astraweb.com> |
| Newsgroups | comp.lang.python |
| Message-ID | <mailman.45.1425500171.21433.python-list@python.org> (permalink) |
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 | Next — Previous in thread | Next in thread | Find similar | Unroll 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