[Tutor] tempfile

Kent Johnson kent37 at tds.net
Fri Dec 24 15:21:09 CET 2004


Marilyn Davis wrote:
> Hello Python Tutors,
> 
> I'm using tempfile.  The doc says it is opened 'w+b' so that it can be
> read and written without closing and reopening.
> 
> But, for the life of me, I can't find any way to rewind it so I can
> read what I wrote.
> 
> tempfile.mkstemp gives me the file descriptor.  Is there a way to make
> a file object from a file descriptor?

Hmm, let's take a look at the docs.
tempfile.TemporaryFile() gives you a file object. Can you use that? Maybe you are using mkstemp() 
because you want the file to persist after it is closed?

Ok, the docs say TemporaryFile() uses mkstemp() to create its file. So how does it get a file 
object? Is the soure available? Sure, in Python24\Lid\tempfile.py. It uses os.fdopen() to convert a 
file descriptor to a file object. Will that work for you?

Kent

> 
> Or, is there something in os or fcntl that allows me to rewind from a
> file descriptor?
> 
> Thank you for any help you can give.
> 
> Marilyn Davis
> 
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
> 


More information about the Tutor mailing list