# Pass "wb" to write a new file, or "ab" to append
with open("test.txt", "wb") as binary_file:
# Write text or bytes to the file
binary_file.write("Write text by encoding
".encode('utf8'))
num_bytes_written = binary_file.write(b'xDExADxBExEF')
print("Wrote %d bytes." % num_bytes_written)