Backporting Python 3 open(encoding="utf-8") to Python 2


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:

Now I'd like to backport this code to Python 2.x, so that I would have a codebase which works with Python 2 and Python 3.

What's the recommended strategy to work around open() differences and lack of encoding parameter?

Could I have a Python 3 open() style file handler which streams bytestrings, so it would act like Python 2 open()?

4/24/2017 6:21:50 PM

Accepted Answer

1. To get an encoding parameter in Python 2:

If you only need to support Python 2.6 and 2.7 you can use instead of open. 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 instead. It also has an encoding parameter, and is quite similar to except it handles line-endings differently.

2. To get a Python 3 open() style file handler which streams bytestrings:

open(filename, 'rb')

Note the 'b', meaning 'binary'.

1/24/2019 3:56:22 PM

I think

from io import open

should do.

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow