When trying to write out a GRIB or BUFR message to a file using codes_write, an error is generated (an exception is thrown)


You have written the following snippet to save a message into a binary file (GRIB or BUFR):

fout = open('temp.out', 'w')
codes_write(msg, fout)

The call to codes_write throws an exception:

Traceback (most recent call last):
  File "", line 85, in <module>
  File "", line 74, in main
  File "", line 61, in example
    codes_write(msg, fout)
TypeError: write() argument must be str, not bytes

Make sure you are using the BINARY MODE for reading and writing
  1. Replace the 'w' with 'wb' in the call to open
  2. fout = open('temp.out', 'wb')
  3. This mode should be used for all files that do not contain text.