您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

TypeError:write()参数必须是str,而不是字节(Python 3 vs Python 2)

5b51 2022/1/14 8:20:16 python 字数 1196 阅读 428 来源 www.jb51.cc/python

下面的代码适用于 python 2.7.13 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

概述

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)所遇到的程序开发问题。


如果您也喜欢它,动动您的小指点个赞吧

除非注明,文章均由 laddyq.com 整理发布,欢迎转载。

转载请注明:
链接:http://laddyq.com
来源:laddyq.com
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。


联系我
置顶