📜  在 python 代码示例中将字节写入文件

📅  最后修改于: 2022-03-11 14:46:31.982000             🧑  作者: Mango

代码示例1
# 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\n".encode('utf8'))
    num_bytes_written = binary_file.write(b'\xDE\xAD\xBE\xEF')
    print("Wrote %d bytes." % num_bytes_written)