2024-05-15 10:41:19 发布
网友
我使用python2.7,我有一个关于读取tempfile的问题。这是我的密码:
import tempfile for i in range(0,10): f = tempfile.NamedTemporaryFile() f.write("Hello") ##f.seek(0) print f.read()
有了这个代码,我得到了这样的结果:
Rワ nize.pyR ゙`Sc d Rワ Rワ Z Z nize.pyR ゙`Sc
这些是什么?你知道吗
谢谢!你知道吗
您正在将字符串写入以字节模式打开的文件。将mode参数添加到对NamedTemporaryFile的调用中:
f = tempfile.NamedTemporaryFile("w")
见https://docs.python.org/2/tutorial/inputoutput.html#reading-and-writing-files
您正在将字符串写入以字节模式打开的文件。将mode参数添加到对NamedTemporaryFile的调用中:
见https://docs.python.org/2/tutorial/inputoutput.html#reading-and-writing-files
相关问题 更多 >
编程相关推荐