yves wrote:
> Kent Johnson a écrit :
>
> Hello,
>
>> Try it like this, using os.fdopen() to convert the low-level file handle
>> from mkstemp() to a Python file object:
>>
>> In [21]: fd, fname = tempfile.mkstemp()
>>
>> In [22]: f = os.fdopen(fd, 'w')
>>
>> In [23]: f.write('foo')
>>
>> In [24]: f.close()
>>
>> In [25]: os.unlink(fname)
>>
>> Seems to work...
>
> Yes, indeed, it works.
> Not so easy for me to understand, though. I think I get it, more or
> less, with the help of the Python tempfile module documentation and the
> help of the Wikipedia article on file descriptors:
> http://en.wikipedia.org/wiki/File_descriptor
OK...the problem was that mkstemp() was opening the file and returning a
low-level object that references the open file. You were opening the
file a second time, so it would have to be closed twice before it could
be deleted.
The object returned by mkstemp is actually just an integer called a file
handle. This is the way C refers to open files. The call to os.fdopen()
wraps the low-level file handle with a Python file object which you can
then use just as if you opened it yourself.
Kent
_______________________________________________
Tutor maillist - [email protected]
http://mail.python.org/mailman/listinfo/tutor