how to switch from os.tmpnam to os.tmpfile

Harold Fellermann dadapapa at
Thu Jun 8 16:44:38 CEST 2006

Maric Michaud wrote:
> Le Jeudi 08 Juin 2006 15:30, Harold Fellermann a écrit :
> > to os.tmpfile() which is supposed to be safer, but I do not know how to
> > get
> > the path information from the file object returned by tmpfile(). any
> > clues?
> There is no path for tmpfile, once it's closed, the file and its content are
> lost. from the doc :
> " The file has no directory entries associated with it and will be
> automatically deleted once there are no file descriptors for the file."
> You must maintain a reference to it in your program untill you don't need it
> anymore.

I am doing so. But still, I need its path. To give you some context:
I have an app built on Tk that uses gnuplot behind the scenes.
My application creates a temporary file where which gnuplot writes its
results to (using the tkcanvas terminal). Later, I load the contents of
that file into the a tk canvas. I don't care about the temporary file
after my app is closed, so I have its reference all the time. But I
its path to tell both gnuplot and tk where to read/write data to/from.

class PlotWindow(Tk.Canvas) :
    def plot(self,commands) :
        tmp = os.tmpnam()
        gnuplot = subprocess.Popen(
            "gnuplot", shell=True,
            stdin=subprocess.PIPE, stdout=file(tmp,"w")
        stdout,stderr = gnuplot.communicate("""
            set terminal tkcanvas interact
            set output "%s"
            """ % tmp + commands)
        assert not stderr"source",tmp)"gnuplot",self._w)

Of course, I could just use matplotlib or but the problem
is not necessary enough to make any refacturing. If there is no way
to use os.tmpfile(), I just go ahead with the security warning. Its
a small personal app, anyway.

- harold -

More information about the Python-list mailing list