Python 尝试写入文件时缓冲区接口出现问题
TypeError:“str”不支持缓冲区接口Python 尝试写入文件时缓冲区接口出现问题,python,Python,TypeError:“str”不支持缓冲区接口 import os,stat fd = os.open("foo.txt",os.O_RDWR|os.O_CREAT) os.write(fd,"This is Test") os.closerange(fd,fd) print("Closed all the files successfully") 您需要写入字节,而不是Unicode字符串: os.write(fd, b"This is Test") b'…'是一个bytes类型的文本。
import os,stat
fd = os.open("foo.txt",os.O_RDWR|os.O_CREAT)
os.write(fd,"This is Test")
os.closerange(fd,fd)
print("Closed all the files successfully")
您需要写入字节,而不是Unicode字符串:
os.write(fd, b"This is Test")
b'…'
是一个bytes
类型的文本。您还可以使用str.encode()
方法对str
类型进行编码:
os.write(fd, "This is Test".encode('ascii'))
一般来说,您希望使用而不是使用
os.open()
和低级文件访问。这可以通过使用。TypeError轻松完成:“str”不支持缓冲区接口