Regarding reading and writing text files in Python, one of the main Python contributors mentions this regarding the surrogateescape
Unicode Error Handler:
Why might the surrogateescape Unicode Error Handler be returning a character that is not ASCII?
Because that's what it explicitly does. That way you can use the same error handler the other way and it will know what to do.
3>> b"'Zo\xc3\xab\\'s'".decode('ascii', errors='surrogateescape')
"'Zo\udcc3\udcab\\'s'"
3>> "'Zo\udcc3\udcab\\'s'".encode('ascii', errors='surrogateescape')
b"'Zo\xc3\xab\\'s'"