概述
import os with open('random.bin','w') as f: f.write(os.urandom(10))
但抛出python 3的错误
3.6.0 | Anaconda 4.3.0(64位)| (默认,2016年12月23日,11:57:41)[MSC v.1900 64 bit(AMD64)]
Traceback (most recent call last): File
“C:/Users/hsingh/PycharmProjects/Item3.py”,line 3,in
f.write(os.urandom(10)) TypeError: write() argument must be str,not bytes
行为不同或如何解决这个问题的原因
with open('random.bin','wb') as f:
这也适用于Python 2.
总结
以上是编程之家为你收集整理的TypeError:write()参数必须是str,而不是字节(Python 3 vs Python 2)全部内容,希望文章能够帮你解决TypeError:write()参数必须是str,而不是字节(Python 3 vs Python 2)所遇到的程序开发问题。
如果您也喜欢它,动动您的小指点个赞吧