473,507 Members | 2,374 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 5412
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
8338
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
5313
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
3131
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
2820
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
1848
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
4660
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
10039
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
1950
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
1403
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
7223
marktang
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,...
0
7111
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7376
jinu1996
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...
1
7031
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...
0
7485
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5623
agi2029
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,...
1
5042
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
3191
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
1
760
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.