'Problem with code compatibility with Python 3.9 - "TypeError: a bytes-like object is required, not 'str'"

While trying to run a telemetry python application on Python 3.9, it fails with the error "TypeError: a bytes-like object is required, not 'str'"

Attempting to fix the code by changing 'r' to 'rb' (and 'w' to 'wb') as suggested elsewhere results in a different error.

Unfortunately, I can't figure this one out. Can anyone help me identify the problem here? I'm very new to this. Thanks in advance.

  def __init__(self):
    try:
      with open(STATUS_FILE, 'r') as sfd:
        self.st_data = pickle.loads(sfd.read())
    except IOError:
      self.st_data = dict(seq=0, timestamp=int(time.time()), rx_packets=0, tx_packets=0)

    self.st_data['seq'] = (self.st_data['seq'] % 999) + 1

  def __repr__(self):
    return "%s %s" % (self.__class__, self.st_data)

  def save(self):
    self.st_data['timestamp'] = int(time.time())
    try:
      with open(STATUS_FILE, 'w') as sfd:
        sfd.write(pickle.dumps(self.st_data))
    except IOError as err:
      print(err)


Solution 1:[1]

pickle.loads() takes a bytes-like object as the first argument. You're sending a string. See: https://docs.python.org/3/library/pickle.html#pickle.loads

Converting a string to bytes is covered in more depth here: Best way to convert string to bytes in Python 3?

The easiest way for you is probably:

my_data = bytes(sfd.read(), 'utf-8')

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 jsfehler