Home > Enterprise >  f.write (data) TypeError: a bytes-like object is required, not 'str'
f.write (data) TypeError: a bytes-like object is required, not 'str'

Time:04-12

enter image description here

if i change data = data payload to data = data str(payload)

I will then have this error
f.write (data) TypeError: a bytes-like object is required, not 'str'

So what can I do to change the code to let the script run

CodePudding user response:

You need to open file in appriopiate mode, default is text mode where str should be provided for writing, whilst b denotes binary mode where bytes is expected, consider following simple examples:

with open("file1.txt","w") as f:
    f.write("HELLO")
with open("file2.txt","wb") as f:
    f.write(b"\x48\x45\x4C\x4C\x4F")

CodePudding user response:

Try to convert it first. data = data.encode()

  • Related