[Tutor] tempfile (fwd)

Marilyn Davis marilyn at deliberate.com
Fri Dec 24 00:11:53 CET 2004

Ooops.  I forgot to send to the list.

---------- Forwarded message ----------
Date: Thu, 23 Dec 2004 14:53:18 -0800 (PST)
From: Marilyn Davis <marilyn at deliberate.com>
To: QoD SEC <qodsec2 at gmail.com>
Subject: Re: [Tutor] tempfile

On Thu, 23 Dec 2004, QoD SEC wrote:

> you could use the seek method of the file object to go to the
> beginning of the file:

Thank you.  But my problem was that I had a "file descriptor", a
low-level thing, and not a "file object", a high-level thing.
A file descriptor doesn't have a seek.

But I found the os.fdopen(fd) call.  It makes a file object from a
file descriptor.  So I can do:

#!/usr/bin/env python
import tempfile
import os
def do_test():
    tmp_fp, tmp_name = tempfile.mkstemp()
    os.write(tmp_fp, "stuff")
    file_obj = os.fdopen(tmp_fp)
    print os.read(tmp_fp,10)



and "stuff" comes out.

But, the mystery now is:

def do_test():
    tmp_fp, tmp_name = tempfile.mkstemp()
    os.write(tmp_fp, "stuff")
    print os.read(tmp_fp,10)

gets an OSError: [Errno 9] Bad File Descriptor

on os.read(tmp_fp,10)

I'm thinking that, if I don't keep a reference to the file object,
it gets automatically closed.

How pythonic.

Thank you for your thoughts.


> here is what the library reference says about it:
> seek(  	offset[, whence])
>     Set the file's current position, like stdio's fseek(). The whence
> argument is optional and defaults to 0 (absolute file positioning);
> other values are 1 (seek relative to the current position) and 2 (seek
> relative to the file's end). There is no return value. Note that if
> the file is opened for appending (mode 'a' or 'a+'), any seek()
> operations will be undone at the next write. If the file is only
> opened for writing in append mode (mode 'a'), this method is
> essentially a no-op, but it remains useful for files opened in append
> mode with reading enabled (mode 'a+'). If the file is opened in text
> mode (mode 't'), only offsets returned by tell() are legal. Use of
> other offsets causes undefined behavior.
>     Note that not all file objects are seekable. 
> from http://docs.python.org/lib/bltin-file-objects.html
> On Thu, 23 Dec 2004 14:17:12 -0800 (PST), Marilyn Davis
> <marilyn at deliberate.com> 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?
> > 
> > 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