Best practice way to open files in Python 2.6+?

python at bdurham.com python at bdurham.com
Tue Jul 27 10:59:42 EDT 2010


Brian,

> As an FYI, the builtin open() uses io.open() on at least 3.1
(maybe also 3.0, don't know). I don't know your use cases or >
what you get or don't get from any of those options, but the
future is io.open.
>
> >>> io.open is open
> True

Under Python 2.6.4 (Windows), "io.open is open" returns False.
Retrieving help() on io.open and open() reinforces that these are
2 different implementations of open.

My use case is reading and writing UTF-8 text files with
universal newline support. I believe that the following io.open()
parameter list is what I should be using:

# mode set to 'rt' (read) or 'wt' (write)
io.open( file, mode, encoding='utf-8', errors='ignore',
newline=None )

Malcolm
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20100727/628ed9f7/attachment.html>


More information about the Python-list mailing list