Hi,
I need to create a temporary file and I need to retrieve the path of
that file.
os.tmpnam() would do the job quite well if it wasn't for the
RuntimeWarning
"tmpnam is a potential security risk to your program". I would like to
switch
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?
thanks!
- harold - 7 8106
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.
--
_____________
Maric Michaud
_____________
Aristote - www.aristote.info
3 place des tapis
69004 Lyon
Tel: +33 426 880 097
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 needit 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 -
Well, I never used gnuplot and I didn't use Tkinter for a while, but :
Le Jeudi 08 Juin 2006 16:44, Harold Fellermann a écrit*: * * * * 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)
assuming tmp is a os.tmpfile and you connect it to the the stdout of your
gnuplot command, something like :
gnuplot = subprocess.Popen(
"gnuplot", shell=True,
stdin=subprocess.PIPE, stdout=tmp
)
stdout,stderr = gnuplot.communicate("""
set terminal tkcanvas interact
set output /dev/stdout
""" % tmp + commands)
should do the job.
* * * * assert not stderr * * * * self.tk.call("source",tmp)
You still need to find a way to pass directly a file object to this call.
Also note if that works, and if you don't need to flush the datas on disk (if
they fit in memory), you can use a file-like buffer (StringIO) instead of a
tmpfile, this will save resources.
This said, you should consider writing your temporary file in a directory
owned by you and not world writeable, which is perfectly safe.
--
_____________
Maric Michaud
_____________
Aristote - www.aristote.info
3 place des tapis
69004 Lyon
Tel: +33 426 880 097
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
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.
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.
regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC/Ltd http://www.holdenweb.com
Love me, love my blog http://holdenweb.blogspot.com
Recent Ramblings http://del.icio.us/steve.holden
Chris Lambacher wrote: You should be able to find exactly what you need in the tempfile module. http://docs.python.org/lib/module-tempfile.html
thanks! tempfile.NamedTemporaryFile() is exaclty what I have been
looking
for. Using python for such a long time now, and still there are unknown
goodies
in the library, great :-)
- harold -
Harold Fellermann <da******@googlemail.com> wrote: I need to create a temporary file and I need to retrieve the path of that file. os.tmpnam() would do the job quite well if it wasn't for the RuntimeWarning "tmpnam is a potential security risk to your program". I would like to switch 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?
You can't. The file opened doesn't have a name
tmpfile(...)
tmpfile() -> file object
Create a temporary file with no directory entries.
Have a look at the functions in the tempfile module, mkstemp() in
particular (the posix way), or NamedTemporaryFile() http://docs.python.org/lib/module-tempfile.html
--
Nick Craig-Wood <ni**@craig-wood.com> -- http://www.craig-wood.com/nick This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Bryan Parkoff |
last post by:
C++ programmers and I tried to experience by writing 65536 switch
cases. It is too large for Windows XP and other operating system to
handle. It looks like that JMP Table obtains 256K bytes for...
|
by: Oliver Knoll |
last post by:
According to my ANSI book, tmpfile() creates a file with wb+ mode
(that is just writing, right?). How would one reopen it for reading?
I got the following (which works):
FILE *tmpFile =...
|
by: Serve Laurijssen |
last post by:
What's the difference between
char buf;
strcpy(buf, tmpnam(NULL));
and
char buf;
tmpnam(buf);
According to my docs both calls write to an internal static buffer. What's
|
by: Josh Wilson |
last post by:
Hey gang,
So I have my stdin which is user defined file, and am wanting to write
it to a temporary file (seems odd I know, but there is a sincere
reason), and then use that file as the stdin for...
|
by: lucas |
last post by:
when i compile programs i download for linux, i get this warning and i can't
finish the compile:
warning: the use of `tmpnam' is dangerous, better use `mkstemp'
is there away around this? or do...
|
by: Steve |
last post by:
I have been trying to find documentation on the behavior
Can anyone tell me why the first example works and the second doesn't
and where I can read about it in the language reference?
Steve
...
|
by: |
last post by:
Is it fine to call another method from Switch?
Eg.
Switch (stringVar)
{
case ("a"):
somVar = "whatever";
Another_Method(); //call another method
return;
|
by: John Machin |
last post by:
Hi,
Here's what's happening:
Python 2.5 (r25:51908, Sep 19 2006, 09:52:17) on win32
Type "help", "copyright", "credits" or "license" for more information.
| >>import os
| >>os.tmpfile()...
|
by: Rohit |
last post by:
Hi,
I am working on a switch module which after reading voltage through a
port pin and caterogizing it into three ranges(open,low or high),
passes this range to a function switch_status() with...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
| |