Python. Как мне преобразовать «дескриптор уровня ОС в открытый файл» в файловый объект?

Вот как это сделать с помощью python-interpreter оператора with:

from __future__ import with_statement
from contextlib import closing
fd, filepath = tempfile.mkstemp()
with closing(os.fdopen(fd, 'w')) as tf:
    tf.write('foo\n')

python

temporary-files

mkstemp

fdopen

2022-10-26T20:31:27+00:00