open(filename, 'rb')
in which case its methods like read()
return Py3 bytes
objects.
On Py2 with future
installed, the builtins
module provides an
open
function that is mostly compatible with that on Python 3 (e.g. it
offers keyword arguments like encoding
). This maps to the open
backport
available in the standard library io
module on Py2.7.
One difference to be aware of between the Python 3 open
and
future.builtins.open
on Python 2 is that the return types of methods such
as read()
from the file object that open
returns are not
automatically cast from native bytes or unicode strings on Python 2 to the
corresponding future.builtins.bytes
or future.builtins.str
types. If you
need the returned data to behave the exactly same way on Py2 as on Py3, you can
cast it explicitly as follows: