permanent tempfile?

C

Colin Wildsmith

Hello,
I am trying to create a temp file, however the file that is created
is still there after the program has completed.
Why is this so?

CoLe

#!/usr/bin/python

import os, tempfile, sys

import tempfile

# creates a random file (text=True is textfile, text=False is binary
file)
ext = '.txt'
pfx = 'tmp'
dir = '/home/argon/PR0001/source/emc2/bin'
filename = tempfile.mkstemp(suffix=ext, prefix=pfx, dir=dir,
text=True)[1]
print filename # eg. C:\Temp\tmpsnrfgk.txt

# test it ...
fout = open(filename, 'w')
fout.write("just a text file")
fout.close()
os.remove(filename)
 
D

Dennis Lee Bieber

filename = tempfile.mkstemp(suffix=ext, prefix=pfx, dir=dir,
text=True)[1]

From the documentation:

mkstemp( [suffix[, prefix[, dir[, text]]]])

Creates a temporary file in the most secure manner possible. There are
no race conditions in the file's creation, assuming that the platform
properly implements the O_EXCL flag for os.open(). The file is readable
and writable only by the creating user ID. If the platform uses
permission bits to indicate whether a file is executable, the file is
executable by no one. The file descriptor is not inherited by child
processes.

Unlike TemporaryFile(), the user of mkstemp() is responsible for
deleting the temporary file when done with it.

Note that second paragraph!


Compare to:

TemporaryFile( [mode='w+b'[, bufsize=-1[, suffix[, prefix[, dir]]]]])

Return a file (or file-like) object that can be used as a temporary
storage area. The file is created using mkstemp. It will be destroyed as
soon as it is closed (including an implicit close when the object is
garbage collected). Under Unix, the directory entry for the file is
removed immediately after the file is created. Other platforms do not
support this; your code should not rely on a temporary file created
using this function having or not having a visible name in the file
system.
--
Wulfraed Dennis Lee Bieber KD6MOG
(e-mail address removed) (e-mail address removed)
HTTP://wlfraed.home.netcom.com/
(Bestiaria Support Staff: (e-mail address removed))
HTTP://www.bestiaria.com/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,773
Messages
2,569,594
Members
45,121
Latest member
LowellMcGu
Top