I have a Python codebase, built for Python 3, which uses Python 3 style open() with encoding parameter:
with open(fname, "rt", encoding="utf-8") as f:
If you only need to support Python 2.6 and 2.7 you can use
io.open instead of
io is the new io subsystem for Python 3, and it exists in Python 2,6 ans 2.7 as well. Please be aware that in Python 2.6 (as well as 3.0) it's implemented purely in python and very slow, so if you need speed in reading files, it's not a good option.
If you need speed, or you need to support Python 2.5 or earlier, you can use
codecs.open instead. It also has an encoding parameter, and is quite similar to
io.open except it handles line-endings differently.
open()style file handler which streams bytestrings:
Note the 'b', meaning 'bytes'.