summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorGuido van Rossum <guido@python.org>1997-12-19 04:29:50 (GMT)
committerGuido van Rossum <guido@python.org>1997-12-19 04:29:50 (GMT)
commitb8c42c9825d57d35149c078cf950ccde1e8802b4 (patch)
tree62042b697fa3bc75ef5df3b6919748e2624683a4 /Lib
parent7d4f68c15f9cd63e26dc5da0795b5ad6a44fe470 (diff)
downloadcpython-b8c42c9825d57d35149c078cf950ccde1e8802b4.zip
cpython-b8c42c9825d57d35149c078cf950ccde1e8802b4.tar.gz
cpython-b8c42c9825d57d35149c078cf950ccde1e8802b4.tar.bz2
Add new optional parameter 'suffix' (default ''), which is appended to
the temporary file name. Also some minor formatting of Jim F's code.
Diffstat (limited to 'Lib')
-rw-r--r--Lib/tempfile.py21
1 files changed, 11 insertions, 10 deletions
diff --git a/Lib/tempfile.py b/Lib/tempfile.py
index d36898b..5b4e388 100644
--- a/Lib/tempfile.py
+++ b/Lib/tempfile.py
@@ -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 @@ counter = 0
# 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 @@ class TemporaryFileWrapper:
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: