how to switch from os.tmpnam to os.tmpfile

Steve Holden steve at
Thu Jun 8 17:14:28 CEST 2006

Harold Fellermann wrote:
> 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
>>>the path information from the file object returned by tmpfile(). any
>>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
> 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
> need
> 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
> 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
> only
> a small personal app, anyway.

Looks like the only way you could handle this is using inherited file 
descriptors between processes and have Gnuplot write to its standard 
output. In other words, build a pipeline like the shell does.

Of course this only works if the process that has the handle on the temp 
file is the same one that starts Gnuplot, but it looks from your code 
like that's the case.

Steve Holden       +44 150 684 7255  +1 800 494 3119
Holden Web LLC/Ltd
Love me, love my blog
Recent Ramblings

More information about the Python-list mailing list