Fix the new compatibility issue in #5407

I had fixed an encoding error in pull request #5407. But it has brought a new compatibility issue. The open BIF in python 2 won't accept the `encoding` parameter and it will occur a `TypeError`. So I create a function to encapsulate the incompatibility.