Back-port open() that accepts an encoding argument. In python3 this uses the built in open() and in python2 this uses the io.open() function. If the file is not being opened in binary mode, then we'll use getpreferredencoding() to find the preferred encoding.
(filename, mode='r', encoding=None, access_permissions=None)
| 199 | |
| 200 | |
| 201 | def compat_open(filename, mode='r', encoding=None, access_permissions=None): |
| 202 | """Back-port open() that accepts an encoding argument. |
| 203 | |
| 204 | In python3 this uses the built in open() and in python2 this |
| 205 | uses the io.open() function. |
| 206 | |
| 207 | If the file is not being opened in binary mode, then we'll |
| 208 | use getpreferredencoding() to find the preferred |
| 209 | encoding. |
| 210 | |
| 211 | """ |
| 212 | opener = os.open |
| 213 | if access_permissions is not None: |
| 214 | opener = partial(os.open, mode=access_permissions) |
| 215 | if 'b' not in mode: |
| 216 | encoding = getpreferredencoding() |
| 217 | return open(filename, mode, encoding=encoding, opener=opener) |
| 218 | |
| 219 | |
| 220 | def bytes_print(statement, stdout=None): |