I need to detect if a filehandle is using binary mode or text mode – this is required in order to be able to encode/decode str/bytes. How can I do that?
When using binary mode myfile.write(bytes) works, and when in text mode myfile.write(str) works.
The idea is that I need to know this in order to be able to encode/decode the argument before calling myfile.write(), otherwise it may fail with an exception.
How about solving your problem this way:
This will work even if your handle does not provide a
.mode.