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