How can I write data to a file when it may contain unknown encoding?

I’m dealing with email headers (like message-id) via the milter protocol in Python 3, and I need to save this data without altering it, even if it’s in an unknown encoding. Using error handlers like ignore or replace makes the headers RFC-compliant, but that reduces the effectiveness of antispam scoring, which depends on the raw input.

In Python 2, my milter application handled this just fine. But in Python 3, when I try to write the raw string to a file, I run into UnicodeEncodeError if the input contains bytes outside UTF-8 (e.g., from ISO8859-2).

Is there a way in Python 3 to write raw data with unknown encoding to disk without raising an exception or altering the content?

Any advice would be appreciated!