我无法从mkstemp
返回的句柄写入由fdopen
作为rw
打开的文件。在
>>> import tempfile
>>> import os
>>> a = tempfile.mkstemp()
>>> b = os.fdopen(a[0], "rw")
>>> b
<open file '<fdopen>', mode 'rw' at 0x7f81ea669f60>
>>> b.write("foo")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IOError: [Errno 9] Bad file descriptor
>>>
奇怪的是,我可以从打开的文件中读取rw
:
如果我以一种或另一种模式打开文件,则一切正常:
>>> c = tempfile.mkstemp()
>>> d = os.fdopen(c[0], "r")
>>> d
<open file '<fdopen>', mode 'r' at 0x2380540>
>>> d.read()
''
>>> e = tempfile.mkstemp()
>>> f = os.fdopen(e[0], "w")
>>> f.write("foo")
>>>
rw
不是有效模式。在如果要以updaing模式(读/写)打开文件,请使用}模式。在
w+
或{(参见^{} documentation :
os.fdopen
的mode
参数与open
相同。)相关问题 更多 >
编程相关推荐