how to switch from os.tmpnam to os.tmpfile

Chris Lambacher chris at kateandchris.net
Thu Jun 8 11:17:31 EDT 2006


You should be able to find exactly what you need in the tempfile module.
http://docs.python.org/lib/module-tempfile.html

os.tmpfile() is no good whether you want the filename or not since on Windows
it is likely to break if you are not a privileged user.  Its a windows
problem, not an actual bug in Python, the call depends on a broken windows
command that creates its files in C:\ 


-Chris

On Thu, Jun 08, 2006 at 07:44:38AM -0700, 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
> > > 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
> 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
>         self.tk.call("source",tmp)
>         self.tk.call("gnuplot",self._w)
> 
> Of course, I could just use matplotlib or Gnuplot.py 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.
> 
> - harold -
> 
> -- 
> http://mail.python.org/mailman/listinfo/python-list



More information about the Python-list mailing list