python 'wb' 模式写入文件怎么输出回车

由于'wb'模式是二进制输入,所以要在''前面加上b才能正常写入.不然会有报错.

TypeError: a bytes-like object is required, not 'str'

环境 python 3.6

f = open('test.txt', 'wb')

for i in range(3):
    f.write(b'hello python\n')

f.close()

猜你喜欢

转载自www.cnblogs.com/crazyzero/p/11126078.html
今日推荐