473,320 Members | 1,707 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

using NamedTemporaryFile on windows


Is there any other reason to use a named tempfile other than
to be able to open it again? I am trying to understand this
section of the documentation regarding NamedTemporaryFile:
"""
Whether the name can be used to open the file a second time, while the named temporary file
is still open, varies across platforms (it can be so used on Unix; it cannot on Windows NT
or later)
"""
From looking through the code, the NamedTemporaryFile will be
deleted as soon as it is closed.

So... if I can't open it again why does it need a name?

Is there a way on windows to make a tempfile that I can open again?

Maybe what I need is just mkstemp, since that also returns a name?
If so, what is the point of NamedTemporaryFile?
Dec 29 '05 #1
5 5404
Lee Harr wrote:
Is there any other reason to use a named tempfile other than
to be able to open it again? I am trying to understand this
section of the documentation regarding NamedTemporaryFile:

"""
Whether the name can be used to open the file a second time, while the named temporary file
is still open, varies across platforms (it can be so used on Unix; it cannot on Windows NT
or later)
"""
As it says, if you *don't close* the file, you can open it again if you
are on a platform which supports that.
From looking through the code, the NamedTemporaryFile will be

deleted as soon as it is closed.

So... if I can't open it again why does it need a name?


Because you can open it again *if* you don't close it... but not on Windows.
Is there a way on windows to make a tempfile that I can open again?
Do you mean open again without having closed it (in other words,
basically get two different file handles to the same open file)? That's
apparently exactly what you cannot do on Windows, as noted above.

Do you mean a file that you can open again *later*, after having closed
it? If so, you obviously don't want a file that is automatically
deleted when you close it, so you just want mkstemp().
Maybe what I need is just mkstemp, since that also returns a name?
If so, what is the point of NamedTemporaryFile?


It creates a file that can be reopened under Unix provided you haven't
closed it yet. ;-)

NamedTemporaryFile doesn't appear to have much purpose on Windows, but
that's more or less what the docs already say. I think the only thing
you were missing perhaps was this idea of "opening a file a second time"
*without having closed it first*.

What I don't understand is why you _can't_ reopen the NamedTemporaryFile
under Windows when you can reopen the file created by mkstemp (and the
files created by TemporaryFile are created by mkstemp in the first place).

-Peter

Dec 29 '05 #2
On 2005-12-29, Peter Hansen <pe***@engcorp.com> wrote:
Lee Harr wrote:
Is there any other reason to use a named tempfile other than
to be able to open it again?
As it says, if you *don't close* the file, you can open it again if you
are on a platform which supports that.

Ok. I just started wondering if maybe there was some other reason,
like stat()ing the file, or taking a picture of Elvis pointing at
it in a directory listing or something.

NamedTemporaryFile doesn't appear to have much purpose on Windows, but
that's more or less what the docs already say. I think the only thing
you were missing perhaps was this idea of "opening a file a second time"
*without having closed it first*.

What I don't understand is why you _can't_ reopen the NamedTemporaryFile
under Windows when you can reopen the file created by mkstemp (and the
files created by TemporaryFile are created by mkstemp in the first place).

Are you saying you tried it and you actually can do what it says
you can't do?

I don't have any windows system to test this, but I want to write
some code that will work on windows. I'm just going to use mkstemp.

Thanks for your time.
Dec 29 '05 #3
On Thu, Dec 29, 2005 at 12:40:34AM -0500, Peter Hansen wrote:

What I don't understand is why you _can't_ reopen the NamedTemporaryFile
under Windows when you can reopen the file created by mkstemp (and the
files created by TemporaryFile are created by mkstemp in the first place).


Basically the only reason that you want a NamedTemporaryFile is so that you
can write something to a file, tell another process to use that file (that you
have just written to) and then have file cleanup taken care of for you
automatically. This works on Unix where you can have process open a
file for reading while another process has the file open for writing. You
can't do this on Windows without jumping through a lot of hoops(sqlite and MS
Access come to mind as programs that manage this, though they may start a
server process to manage it).

mkstemp does create a file for you, but you are responsible for removing the
file when you are done. Unfortunately this is what you are left with on
Windows.

-Chris
Dec 29 '05 #4
Lee Harr wrote:
On 2005-12-29, Peter Hansen <pe***@engcorp.com> wrote:
What I don't understand is why you _can't_ reopen the NamedTemporaryFile
under Windows when you can reopen the file created by mkstemp (and the
files created by TemporaryFile are created by mkstemp in the first place).


Are you saying you tried it and you actually can do what it says
you can't do?


I don't think so. I think I was saying that I can do exactly what it
says I can, and can't do what it says I can't do, but that I don't
understand why there is a difference between the two approaches given
what else it says... (I hope that's clearer than it looks to me. ;-)

I did try it, and I can't reopen the NamedTemporaryFile on Windows, but
I can reopen the mkstemp file. Both from the same process, which could
be quite different than what the docs are actually talking about (noting
Chris' reply).

-Peter

Dec 30 '05 #5
[Peter Hansen]
What I don't understand is why you _can't_ reopen the NamedTemporaryFile
under Windows when you can reopen the file created by mkstemp (and the
files created by TemporaryFile are created by mkstemp in the first place).

[Lee Harr] Are you saying you tried it and you actually can do what it says
you can't do?

[Peter Hansen] I don't think so. I think I was saying that I can do exactly what it
says I can, and can't do what it says I can't do, but that I don't
understand why there is a difference between the two approaches given
what else it says... (I hope that's clearer than it looks to me. ;-)


Because NamedTemporaryFile on Windows passes the Microsoft-specific
O_TEMPORARY flag, and mkstemp doesn't. One consequence is that you
have to delete a temp file obtained from mkstemp yourself, but a
NamedTemporaryFile goes away by magic when the last handle to it is
closed. Microsoft's I/O libraries do that cleanup, not Python.

Another consequence is that a file opened with O_TEMPORARY can't be
opened again (at least not via C stdio), not even by the process that
opened it to begin with. AFAICT Microsoft never documented this, but
that's how it works. Because you can't delete an open file on
Windows, temp files on Windows always have names visible in the
filesystem, and that's a potential "security risk". _Presumably_ the
inability to open an O_TEMPORARY file again was a partially-baked
approach to eliminating that risk.
Dec 30 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: grutta | last post by:
I am writing a windows service that will recieve notification when a USB Device is insterted into the machine. I have used the RegisterDeviceNotification and the RegisterServiceCtrlHandlerEx with...
3
by: Rob | last post by:
Hi all, I am having trouble converting the code below (found on http://vbnet.mvps.org/index.html?code/core/sendmessage.htm) into a format that will work using vb .NET. Can anyone have a look...
1
by: Gerard Flanagan | last post by:
Hello I'm sure its basic but I'm confused about the error I get with the following code. Any help on basic tempfile usage? ActivePython 2.4.1 Build 247 (ActiveState Corp.) based on Python...
3
by: Siv | last post by:
Hi, A little while ago I wrote a small program that allowed the user to view products from a database. The database holds the details of the products which can be viewed via a form and...
2
by: Jason Lunz | last post by:
Is there a better way to do this? def QuietNamedTemporaryFile(**kwargs): ''' Return a NamedTemporaryFile that doesn't complain when its file has already been unlinked at __del__ time. ''' ...
6
by: Imbaud Pierre | last post by:
On suse 9.3, tempfile.NamedTemporaryFile() doesnt work as expected. (I found a permanent workaround, so I dont ask for help) I expected to write to a file, and access it thru a shell command. This...
2
by: rparimi | last post by:
I am trying to redirect stderr of a process to a temporary file and then read back the contents of the file, all in the same python script. As a simple exercise, I launched /bin/ls but this doesn't...
1
by: rparimi | last post by:
Hello pythoners, When I create temporary file using the tempfile module, and forkI) later on in my program, I always see errors when the program exits. Is this because the child process deletes...
3
by: Michael Hoffman | last post by:
I am writing a library that creates temporary files and calls a series of external programs to process these files. Sometimes these external programs create files in the same directory as the input...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.