mirror of https://github.com/python/cpython.git
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:
parent
7d4f68c15f
commit
b8c42c9825
|
@ -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
|
||||
|
||||
|
@ -113,8 +114,8 @@ def __getattr__(self, name):
|
|||
return a
|
||||
|
||||
|
||||
def TemporaryFile(mode='w+b', bufsize=-1):
|
||||
name=mktemp()
|
||||
def TemporaryFile(mode='w+b', bufsize=-1, suffix=""):
|
||||
name = mktemp(suffix)
|
||||
file = open(name, mode, bufsize)
|
||||
try:
|
||||
os.unlink(name)
|
||||
|
|
Loading…
Reference in New Issue