‘str' does not support the buffer interface

来源:互联网 发布:郑州直销软件开发 编辑:程序博客网 时间:2024/05/19 23:23

python3 ‘str’ does not support the buffer interface解决方案

由于 python2 和 python3 对string 类型的类型是不一样的。
在import os模块时,若是选用 os.open,os.write等操作文件。则会出现问题。

解决方法一:

fd = os.open('a.txt',os.O_CREAT|os.O_RDWR)n = os.write(fd,b ' write text ' ) #在要写入的内容前加一个 b

解决方法二:

fd = os.open('a.txt',os.O_CREAT|os.O_RDWR)buf = ' write text 'n = os.write(fd , buf.encode(' utf-8 '))  #若写入的是一个buf,则调用 encode 函数变成utf-8编码即可

解决方法三:

直接运用,open,read,write函数,而不是 os 模块中的。则不会出现上述问题

阅读全文
0 0
原创粉丝点击