tempfile.mkstemp and os.fdopen

billiejoex gnewsg at gmail.com
Wed Aug 29 03:21:12 CEST 2007

Thanks all.
Another question: I have to open file for writing ('wb') but I noticed
that both tempfile.mkstemp() and os.fdopen() accept a "mode" argument.
It's not clear *when* do I have to specify such mode. When using

>>> fileno, name = tempfile.mkstemp(text=False)
>>> fd = os.fdopen(fileno)

...or when using os.fdopen()?

>>> fileno, name = tempfile.mkstemp()
>>> fd = os.fdopen(fileno, mode='wb')

Moreover, what happens if I specify "text" mode when using mkstemp and
"binary" mode when using fdopen?

>>> fileno, name = tempfile.mkstemp(text=True)
>>> fd = os.fdopen(fileno, 'wb')

PS - I think that tempfile.mkstemp docstring should be enhanced to
cover such and other questions (e.g. I find reasonable that every user
using tempfile.mkstemp() should use also os.fdopen() in conjunction
but this isn't mentioned).

More information about the Python-list mailing list