Add new optional parameter 'suffix' (default ''), which is appended to

the temporary file name.  Also some minor formatting of Jim F's code.
This commit is contained in:
Guido van Rossum 1997-12-19 04:29:50 +00:00
parent 7d4f68c15f
commit b8c42c9825
1 changed files with 11 additions and 10 deletions

View File

@ -27,7 +27,8 @@ def gettempdir():
elif os.name == 'mac':
import macfs, MACFS
try:
refnum, dirid = macfs.FindFolder(MACFS.kOnSystemDisk, MACFS.kTemporaryFolderType, 0)
refnum, dirid = macfs.FindFolder(MACFS.kOnSystemDisk,
MACFS.kTemporaryFolderType, 0)
dirname = macfs.FSSpec((refnum, dirid, '')).as_pathname()
attempdirs.insert(0, dirname)
except macfs.error:
@ -76,13 +77,13 @@ def gettempprefix():
# User-callable function to return a unique temporary file name
def mktemp():
def mktemp(suffix=""):
global counter
dir = gettempdir()
pre = gettempprefix()
while 1:
counter = counter + 1
file = os.path.join(dir, pre + `counter`)
file = os.path.join(dir, pre + `counter` + suffix)
if not os.path.exists(file):
return file
@ -95,8 +96,8 @@ class TemporaryFileWrapper:
no longer needed.
"""
def __init__(self, file, path):
self.file=file
self.path=path
self.file = file
self.path = path
def close(self):
self.file.close()
@ -107,15 +108,15 @@ def __del__(self):
except: pass
def __getattr__(self, name):
file=self.__dict__['file']
a=getattr(file, name)
file = self.__dict__['file']
a = getattr(file, name)
setattr(self, name, a)
return a
def TemporaryFile(mode='w+b', bufsize=-1):
name=mktemp()
file=open(name,mode,bufsize)
def TemporaryFile(mode='w+b', bufsize=-1, suffix=""):
name = mktemp(suffix)
file = open(name, mode, bufsize)
try:
os.unlink(name)
except os.error: