tempfile.mkstemp and os.fdopen

Gerard Flanagan grflanagan at yahoo.co.uk
Wed Aug 29 03:37:15 EDT 2007


On Aug 28, 7:55 pm, billiejoex <gne... at gmail.com> wrote:
> Hi there.
> I'm trying to generate a brand new file with a unique name by using
> tempfile.mkstemp().
> In conjunction I used os.fdopen() to get a wrapper around file
> properties (write & read methods, and so on...) but 'name' attribute
> does not contain the correct file name. Why?
>

import tempfile, os

class TempFile(object):

    def __init__(self, fd, fname):
        self._fileobj = os.fdopen(fd, 'w+b')
        self.name = fname

    def __getattr__(self, attr):
        return getattr(self._fileobj, attr)

def mktempfile(dir=None, suffix='.tmp'):
    return TempFile(*tempfile.mkstemp(dir=dir, suffix=suffix))








More information about the Python-list mailing list