所以我想用Python制作一个二进制文件,但它给出了一个错误
TypeError: a bytes-like object is required, not 'str' Here is my code
with open('test.binary','wb') as f:
f = f.write('Hello!')
有人可以帮忙吗?
最佳答案
如果以二进制方式打开文件,则必须将字符串编码为字节:
with open('test.binary','wb') as f:
f = f.write('Hello!'.encode())
或者使用 b
作为字符串的前缀:
with open('test.binary','wb') as f:
f = f.write(b'Hello!')
关于python - 我正在用 python 编程,但我一直在制作二进制文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70394956/