我无法通过rw
从fdopen
返回的句柄写入mkstemp
打开的文件。
>>> 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
文件中读取:
>>> g = tempfile.mkstemp()
>>> h = os.fdopen(g[0], "rw")
>>> h.read()
''
如果我以一种模式或另一种模式打开文件,那么事情就可以了:
>>> 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")
>>>
答案 0 :(得分:3)