Python – Error! blahfile is not UTF-8 encoded. Saving disabled

binary-datagzipjupyterjupyter-notebookpython

So, I'm trying to write a gzip file, actually from the net, but to simplify I wrote some very basic test.

import gzip
LINES = [b'I am a test line' for _ in range(100_000)]
f = gzip.open('./test.text.gz', 'wb')
for line in LINES:
    f.write(line)
f.close()

It runs great, and I can see in Jupyter that it has created the test.txt.gz file in the directory listing. So I click on it expecting a whole host of garbage characters indicative of a binary file, like you would see in Notepad.
However, instead I get this …

Error!  test.text.gz is not UTF-8 encoded.
Saving disabled.
See console for more details

Which makes me think, oh my god, coding error, something is wrong with my encoding, my saving, can I save bytes ? Am I using the correct routines ?? And then spend 5 hours trying all combinations of code and modules.

Best Answer

The very simple answer to this is none of the above. This is a very misleading error message, especially when the code you've written was designed to save a binary file with a weird extension.

What this actually means is ...

    I HAVE NO IDEA HOW TO DISPLAY THIS DATA ! - Yours Jupyter

So, go to your File Explorer, Finder navigate to the just saved file and open it. Voila !! Everything worked exactly as planned, there is no error.

Hope this saves other people many hours of debugging, and please Jupyter, change your error message.