python – How to write bytes to file?

python – How to write bytes to file?

If you want to write bytes then you should open the file in binary mode.

f = open(/tmp/output, wb)

Write bytes and Create the file if not exists:

f = open(./put/your/path/here.png, wb)
f.write(data)
f.close()

wb means open the file in write binary mode.

python – How to write bytes to file?

Here is just a cleaner version with with :

with open(filename, wb) as f: 
    f.write(filebytes)

Leave a Reply

Your email address will not be published.