• Uncategorized

About python : fwrite-data-TypeError-a-bytes-like-object-is-required-not-str

Question Detail

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

Question Answer

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")

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

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.