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)
CodePudding user response:
Using binary file modes is the correct thing to do. Making your code into a reproducible example with those changes works. Make sure to delete any STATUS_FILE created by the original non-working code:
STATUS_FILE = 'test.pkl' # defined missing variable
class Test: # added shown methods to a class
def __init__(self):
try:
with open(STATUS_FILE, 'rb') as sfd: # use binary mode
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, 'wb') as sfd: # use binary mode
sfd.write(pickle.dumps(self.st_data))
except IOError as err:
print(err)
t = Test() # instantiate class, read from save file if present
print(t) # view the result
t.save() # create the save file
Output from multiple runs. Sequence increments and timestamp changes.
<class '__main__.Test'> {'seq': 1, 'timestamp': 1644104029, 'rx_packets': 0, 'tx_packets': 0}
<class '__main__.Test'> {'seq': 2, 'timestamp': 1644104029, 'rx_packets': 0, 'tx_packets': 0}
<class '__main__.Test'> {'seq': 3, 'timestamp': 1644104030, 'rx_packets': 0, 'tx_packets': 0}
<class '__main__.Test'> {'seq': 4, 'timestamp': 1644104030, 'rx_packets': 0, 'tx_packets': 0}
<class '__main__.Test'> {'seq': 5, 'timestamp': 1644104031, 'rx_packets': 0, 'tx_packets': 0}
CodePudding user response:
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')
