tempfile.mkstemp()回報:Python - 如何將「操作系統級句柄轉換爲打開文件」轉換爲文件對象?
含有OS級句柄打開的文件和該文件的絕對路徑名(如將由os.open()被返回),在該順序的元組。
如何將該OS級句柄轉換爲文件對象?
的documentation for os.open()狀態:
包裝一個文件描述符在一個 「文件 對象」,使用fdopen()。
所以,我想:
>>> import tempfile
>>> tup = tempfile.mkstemp()
>>> import os
>>> f = os.fdopen(tup[0])
>>> f.write('foo\n')
Traceback (most recent call last):
File "<stdin>", line 1, in ?
IOError: [Errno 9] Bad file descriptor
切記要一個答案,「接受」,如果它爲你工作。 – 2008-10-03 20:18:27